<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0"><meta name="theme-color" content="#0078E7"><meta name="author" content="zzl"><meta name="copyright" content="zzl"><meta name="generator" content="Hexo 5.4.0"><meta name="theme" content="hexo-theme-yun"><title>java初级 | zzl</title><link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@900&amp;display=swap" media="print" onload="this.media='all'"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/star-markdown-css@0.1.25/dist/yun/yun-markdown.min.css"><script src="//at.alicdn.com/t/font_1140697_dxory92pb0h.js" async></script><script src="https://cdn.jsdelivr.net/npm/scrollreveal/dist/scrollreveal.min.js" defer></script><script>document.addEventListener("DOMContentLoaded", () => {
  [".post-card",".post-content img"].forEach((target)=> {
    ScrollReveal().reveal(target);
  })
});
</script><link rel="icon" href="/web/yun.svg"><link rel="mask-icon" href="/web/yun.svg" color="#0078E7"><link rel="alternate icon" href="/web/yun.ico"><link rel="preload" href="/web/css/hexo-theme-yun.css" as="style"><link rel="preload" href="/web/js/utils.js" as="script"><link rel="preload" href="/web/js/hexo-theme-yun.js" as="script"><link rel="prefetch" href="/web/js/sidebar.js" as="script"><link rel="preconnect" href="https://cdn.jsdelivr.net" crossorigin><script id="yun-config">
    const Yun = window.Yun || {};
    window.CONFIG = {"hostname":"zilongzeng.gitee.io","root":"/web/","title":"奏者的小站","version":"1.6.2","mode":"auto","copycode":true,"page":{"isPost":true},"i18n":{"placeholder":"Searching...","empty":"We didn't find any results for the search: ${query}.","hits":"${hits} results found","hits_time":"${hits} results found in ${time} ms"},"anonymous_image":"https://cdn.jsdelivr.net/gh/YunYouJun/cdn/img/avatar/none.jpg","say":{"api":"https://v1.hitokoto.cn","hitokoto":true},"fireworks":{"colors":["102, 167, 221","62, 131, 225","33, 78, 194"]}};
  </script><link rel="stylesheet" href="/web/css/hexo-theme-yun.css"><script src="/web/js/utils.js"></script><script src="/web/js/hexo-theme-yun.js"></script><meta name="description" content="java初级概念及知识点">
<meta property="og:type" content="article">
<meta property="og:title" content="java初级">
<meta property="og:url" content="http://zilongzeng.gitee.io/web/2022/09/13/java%E5%88%9D%E7%BA%A7/index.html">
<meta property="og:site_name" content="zzl">
<meta property="og:description" content="java初级概念及知识点">
<meta property="og:locale" content="en_US">
<meta property="og:image" content="http://zilongzeng.gitee.io/web/2022/09/13/images/jdk1.7%E5%92%8Cjdk1.8%E5%86%85%E5%AD%98%E7%BB%93%E6%9E%84%E5%AF%B9%E6%AF%94.png">
<meta property="og:image" content="http://zilongzeng.gitee.io/web/2022/09/13/images/java%E5%86%85%E5%AD%98%E8%AF%A6%E7%BB%86%E5%9B%BE.png">
<meta property="article:published_time" content="2022-09-13T08:09:37.000Z">
<meta property="article:modified_time" content="2022-09-13T08:20:32.794Z">
<meta property="article:author" content="zzl">
<meta property="article:tag" content="java">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="http://zilongzeng.gitee.io/web/2022/09/13/images/jdk1.7%E5%92%8Cjdk1.8%E5%86%85%E5%AD%98%E7%BB%93%E6%9E%84%E5%AF%B9%E6%AF%94.png"><script src="/web/js/ui/mode.js"></script></head><body><script defer src="https://cdn.jsdelivr.net/npm/animejs@latest"></script><script defer src="/web/js/ui/fireworks.js"></script><canvas class="fireworks"></canvas><div class="container"><a class="sidebar-toggle hty-icon-button" id="menu-btn"><div class="hamburger hamburger--spin" type="button"><span class="hamburger-box"><span class="hamburger-inner"></span></span></div></a><div class="sidebar-toggle sidebar-overlay"></div><aside class="sidebar"><script src="/web/js/sidebar.js"></script><ul class="sidebar-nav"><li class="sidebar-nav-item sidebar-nav-toc hty-icon-button sidebar-nav-active" data-target="post-toc-wrap" title="Table of Contents"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-list-ordered"></use></svg></li><li class="sidebar-nav-item sidebar-nav-overview hty-icon-button" data-target="site-overview-wrap" title="Overview"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-passport-line"></use></svg></li></ul><div class="sidebar-panel" id="site-overview-wrap"><div class="site-info fix-top"><a class="site-author-avatar" href="/web/about/" title="zzl"><img width="96" loading="lazy" src="/web/zzl.jpg" alt="zzl"><span class="site-author-status" title="永远相信美好的事情即将发生">😊</span></a><div class="site-author-name"><a href="/web/about/">zzl</a></div><span class="site-name">zzl</span><sub class="site-subtitle"></sub><div class="site-desciption"></div></div><nav class="site-state"><a class="site-state-item hty-icon-button icon-home" href="/web/" title="Home"><span class="site-state-item-icon"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-home-4-line"></use></svg></span></a><div class="site-state-item"><a href="/web/archives/" title="Archives"><span class="site-state-item-icon"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-archive-line"></use></svg></span><span class="site-state-item-count">16</span></a></div><div class="site-state-item"><a href="/web/categories/" title="Categories"><span class="site-state-item-icon"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-folder-2-line"></use></svg></span><span class="site-state-item-count">0</span></a></div><div class="site-state-item"><a href="/web/tags/" title="Tags"><span class="site-state-item-icon"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-price-tag-3-line"></use></svg></span><span class="site-state-item-count">9</span></a></div><a class="site-state-item hty-icon-button" target="_blank" rel="noopener" href="https://yun.yunyoujun.cn" title="文档"><span class="site-state-item-icon"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-settings-line"></use></svg></span></a></nav><hr style="margin-bottom:0.5rem"><div class="links-of-author"><a class="links-of-author-item hty-icon-button" rel="noopener" href="/atom.xml" title="RSS" target="_blank" style="color:orange"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-rss-line"></use></svg></a><a class="links-of-author-item hty-icon-button" rel="noopener" title="QQ 2915667803" target="_blank" style="color:#12B7F5"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-qq-line"></use></svg></a><a class="links-of-author-item hty-icon-button" rel="noopener" title="GitHub" target="_blank" style="color:#6e5494"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-github-line"></use></svg></a><a class="links-of-author-item hty-icon-button" rel="noopener" title="微博" target="_blank" style="color:#E6162D"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-weibo-line"></use></svg></a><a class="links-of-author-item hty-icon-button" rel="noopener" title="豆瓣" target="_blank" style="color:#007722"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-douban-line"></use></svg></a><a class="links-of-author-item hty-icon-button" rel="noopener" title="网易云音乐" target="_blank" style="color:#C20C0C"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-netease-cloud-music-line"></use></svg></a><a class="links-of-author-item hty-icon-button" rel="noopener" title="知乎" target="_blank" style="color:#0084FF"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-zhihu-line"></use></svg></a><a class="links-of-author-item hty-icon-button" rel="noopener" title="哔哩哔哩" target="_blank" style="color:#FF8EB3"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-bilibili-line"></use></svg></a><a class="links-of-author-item hty-icon-button" rel="noopener" title="微信公众号" target="_blank" style="color:#1AAD19"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-wechat-2-line"></use></svg></a><a class="links-of-author-item hty-icon-button" rel="noopener" title="Twitter" target="_blank" style="color:#1da1f2"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-twitter-line"></use></svg></a><a class="links-of-author-item hty-icon-button" rel="noopener" title="Telegram Channel" target="_blank" style="color:#0088CC"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-telegram-line"></use></svg></a><a class="links-of-author-item hty-icon-button" rel="noopener" title="E-Mail" target="_blank" style="color:#8E71C1"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-mail-line"></use></svg></a><a class="links-of-author-item hty-icon-button" rel="noopener" title="Travelling" target="_blank" style="color:var(--hty-text-color)"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-train-line"></use></svg></a></div><hr style="margin:0.5rem 1rem"><div class="links"><a class="links-item hty-icon-button" href="/web/links/" title="我的小伙伴们" style="color:dodgerblue"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-genderless-line"></use></svg></a></div><br><a class="links-item hty-icon-button" id="toggle-mode-btn" href="javascript:;" title="Mode" style="color: #f1cb64"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-contrast-2-line"></use></svg></a></div><div class="sidebar-panel sidebar-panel-active" id="post-toc-wrap"><div class="post-toc"><div class="post-toc-content"><ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link" href="#%E5%9F%BA%E7%A1%80%E7%9F%A5%E8%AF%86"><span class="toc-number">1.</span> <span class="toc-text">基础知识</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E8%AE%A1%E7%AE%97%E6%9C%BA%E5%B8%B8%E8%AF%86"><span class="toc-number">1.1.</span> <span class="toc-text">计算机常识</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E8%AE%A1%E7%AE%97%E6%9C%BA%E7%9A%84%E4%BA%8C%E8%BF%9B%E5%88%B6"><span class="toc-number">1.2.</span> <span class="toc-text">计算机的二进制</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E4%BA%8C%E8%BF%9B%E5%88%B6%E7%9A%84%E5%8A%A0%E5%87%8F%E6%B3%95"><span class="toc-number">1.2.1.</span> <span class="toc-text">二进制的加减法</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E4%BA%8C%E8%BF%9B%E5%88%B6%E7%9A%84%E5%8E%9F%E7%A0%81%EF%BC%8C%E5%8F%8D%E7%A0%81%EF%BC%8C%E8%A1%A5%E7%A0%81"><span class="toc-number">1.2.2.</span> <span class="toc-text">二进制的原码，反码，补码</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E4%BA%8C%E8%BF%9B%E5%88%B6%E7%9A%84%E9%80%BB%E8%BE%91%E8%BF%90%E7%AE%97"><span class="toc-number">1.2.3.</span> <span class="toc-text">二进制的逻辑运算</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E4%B8%8E%E8%BF%90%E7%AE%97"><span class="toc-number">1.2.3.1.</span> <span class="toc-text">与运算</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E6%88%96%E8%BF%90%E7%AE%97"><span class="toc-number">1.2.3.2.</span> <span class="toc-text">或运算</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E9%9D%9E%E8%BF%90%E7%AE%97"><span class="toc-number">1.2.4.</span> <span class="toc-text">非运算</span></a></li></ol></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#java%E5%85%A5%E9%97%A8"><span class="toc-number">2.</span> <span class="toc-text">java入门</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#java%E6%A6%82%E8%BF%B0"><span class="toc-number">2.1.</span> <span class="toc-text">java概述</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E4%BB%80%E4%B9%88%E6%98%AF-java"><span class="toc-number">2.1.1.</span> <span class="toc-text">什么是 java</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#java-%E8%83%BD%E5%81%9A%E4%BB%80%E4%B9%88"><span class="toc-number">2.1.2.</span> <span class="toc-text">java 能做什么</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#java-%E7%9A%84%E5%8E%86%E5%8F%B2"><span class="toc-number">2.1.3.</span> <span class="toc-text">java 的历史</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#java-%E7%9A%84%E8%AF%AD%E8%A8%80%E7%89%B9%E7%82%B9"><span class="toc-number">2.1.4.</span> <span class="toc-text">java 的语言特点</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#java-%E7%9A%84%E5%9F%BA%E7%A1%80%E4%BD%BF%E7%94%A8"><span class="toc-number">2.2.</span> <span class="toc-text">java 的基础使用</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E7%BC%96%E5%86%99%E7%AC%AC%E4%B8%80%E4%B8%AA-HelloWorld-java-%E6%96%87%E4%BB%B6"><span class="toc-number">2.2.1.</span> <span class="toc-text">编写第一个 HelloWorld.java 文件</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#java-%E4%B8%AD%E7%9A%84%E6%A0%87%E8%AF%86%E7%AC%A6%E5%92%8C%E6%B3%A8%E9%87%8A"><span class="toc-number">2.3.</span> <span class="toc-text">java 中的标识符和注释</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%A0%87%E8%AF%86%E7%AC%A6"><span class="toc-number">2.3.1.</span> <span class="toc-text">标识符</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%B3%A8%E9%87%8A"><span class="toc-number">2.3.2.</span> <span class="toc-text">注释</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#java-%E4%B8%AD%E7%9A%84%E5%8F%98%E9%87%8F%E5%92%8C%E8%BF%90%E7%AE%97%E7%AC%A6%E5%8F%B7"><span class="toc-number">2.4.</span> <span class="toc-text">java 中的变量和运算符号</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#java-%E4%B8%AD%E7%9A%84%E6%95%B0%E6%8D%AE%E7%B1%BB%E5%9E%8B"><span class="toc-number">2.4.1.</span> <span class="toc-text">java 中的数据类型</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E6%95%B4%E5%9E%8B"><span class="toc-number">2.4.1.1.</span> <span class="toc-text">整型</span></a><ol class="toc-child"><li class="toc-item toc-level-5"><a class="toc-link" href="#%E6%95%B4%E5%9E%8B%E7%9A%84%E5%8F%96%E5%80%BC"><span class="toc-number">2.4.1.1.1.</span> <span class="toc-text">整型的取值</span></a></li></ol></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E6%B5%AE%E7%82%B9%E5%9E%8B"><span class="toc-number">2.4.1.2.</span> <span class="toc-text">浮点型</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%AD%97%E7%AC%A6%E5%9E%8B"><span class="toc-number">2.4.1.3.</span> <span class="toc-text">字符型</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%B8%83%E5%B0%94%E5%9E%8B"><span class="toc-number">2.4.1.4.</span> <span class="toc-text">布尔型</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%9F%BA%E6%9C%AC%E6%95%B0%E6%8D%AE%E7%B1%BB%E5%9E%8B%E6%80%BB%E7%BB%93"><span class="toc-number">2.4.1.5.</span> <span class="toc-text">基本数据类型总结</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#java-%E4%B8%AD%E7%9A%84%E8%BF%90%E7%AE%97%E7%AC%A6%E5%8F%B7"><span class="toc-number">2.4.2.</span> <span class="toc-text">java 中的运算符号</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E7%AE%97%E6%95%B0%E8%BF%90%E7%AE%97%E7%AC%A6"><span class="toc-number">2.4.2.1.</span> <span class="toc-text">算数运算符</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E8%B5%8B%E5%80%BC%E8%BF%90%E7%AE%97%E7%AC%A6"><span class="toc-number">2.4.2.2.</span> <span class="toc-text">赋值运算符</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E6%AF%94%E8%BE%83%E8%BF%90%E7%AE%97%E7%AC%A6"><span class="toc-number">2.4.2.3.</span> <span class="toc-text">比较运算符</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E9%80%BB%E8%BE%91%E8%BF%90%E7%AE%97%E7%AC%A6"><span class="toc-number">2.4.2.4.</span> <span class="toc-text">逻辑运算符</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E4%B8%89%E5%85%83%E8%BF%90%E7%AE%97%E7%AC%A6"><span class="toc-number">2.4.2.5.</span> <span class="toc-text">三元运算符</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E4%BD%8D%E8%BF%90%E7%AE%97%E7%AC%A6"><span class="toc-number">2.4.2.6.</span> <span class="toc-text">位运算符</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#java-%E4%B8%AD%E7%9A%84%E7%B1%BB%E5%9E%8B%E8%BD%AC%E6%8D%A2"><span class="toc-number">2.4.3.</span> <span class="toc-text">java 中的类型转换</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#java-%E4%B8%AD%E7%9A%84%E5%B8%B8%E7%94%A8%E5%BC%95%E7%94%A8%E7%B1%BB%E5%9E%8B"><span class="toc-number">2.5.</span> <span class="toc-text">java 中的常用引用类型</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#java-%E4%B8%AD%E7%9A%84%E6%B5%81%E7%A8%8B%E6%8E%A7%E5%88%B6%E8%AF%AD%E5%8F%A5"><span class="toc-number">2.6.</span> <span class="toc-text">java 中的流程控制语句</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E9%A1%BA%E5%BA%8F%E7%BB%93%E6%9E%84"><span class="toc-number">2.6.1.</span> <span class="toc-text">顺序结构</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%88%86%E6%94%AF%E7%BB%93%E6%9E%84"><span class="toc-number">2.6.2.</span> <span class="toc-text">分支结构</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#if%E5%88%86%E6%94%AF%E8%AF%AD%E5%8F%A5"><span class="toc-number">2.6.2.1.</span> <span class="toc-text">if分支语句</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#switch%E5%88%86%E6%94%AF%E8%AF%AD%E5%8F%A5"><span class="toc-number">2.6.2.2.</span> <span class="toc-text">switch分支语句</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E9%80%89%E6%8B%A9%E5%88%86%E6%94%AF%E6%80%BB%E7%BB%93"><span class="toc-number">2.6.2.3.</span> <span class="toc-text">选择分支总结</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%BE%AA%E7%8E%AF%E7%BB%93%E6%9E%84"><span class="toc-number">2.6.3.</span> <span class="toc-text">循环结构</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#for-%E5%BE%AA%E7%8E%AF"><span class="toc-number">2.6.3.1.</span> <span class="toc-text">for 循环</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#while-%E5%BE%AA%E7%8E%AF"><span class="toc-number">2.6.3.2.</span> <span class="toc-text">while 循环</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%BE%AA%E7%8E%AF%E4%B8%AD%E7%9A%84%E7%89%B9%E6%AE%8A%E6%83%85%E5%86%B5"><span class="toc-number">2.6.3.3.</span> <span class="toc-text">循环中的特殊情况</span></a></li></ol></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#java-%E4%B8%AD%E7%9A%84%E6%95%B0%E7%BB%84"><span class="toc-number">2.7.</span> <span class="toc-text">java 中的数组</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%95%B0%E7%BB%84%E7%9A%84%E5%9F%BA%E6%9C%AC%E6%A6%82%E5%BF%B5"><span class="toc-number">2.7.1.</span> <span class="toc-text">数组的基本概念</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%95%B0%E7%BB%84%E7%BB%83%E4%B9%A0"><span class="toc-number">2.7.2.</span> <span class="toc-text">数组练习</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#IDEA-%E4%B8%AD%E7%9A%84%E8%B0%83%E8%AF%95%E5%8A%9F%E8%83%BD"><span class="toc-number">2.7.3.</span> <span class="toc-text">IDEA 中的调试功能</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%88%9D%E8%AF%86%E7%AE%97%E6%B3%95"><span class="toc-number">2.7.4.</span> <span class="toc-text">初识算法</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%86%92%E6%B3%A1%E6%8E%92%E5%BA%8F"><span class="toc-number">2.7.4.1.</span> <span class="toc-text">冒泡排序</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E9%80%89%E6%8B%A9%E6%8E%92%E5%BA%8F"><span class="toc-number">2.7.4.2.</span> <span class="toc-text">选择排序</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E6%8F%92%E5%85%A5%E6%8E%92%E5%BA%8F"><span class="toc-number">2.7.4.3.</span> <span class="toc-text">插入排序</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E4%BA%8C%E5%88%86%E6%9F%A5%E6%89%BE"><span class="toc-number">2.7.4.4.</span> <span class="toc-text">二分查找</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E7%AE%97%E6%B3%95%E7%9A%84%E6%80%A7%E8%83%BD"><span class="toc-number">2.7.4.5.</span> <span class="toc-text">算法的性能</span></a><ol class="toc-child"><li class="toc-item toc-level-5"><a class="toc-link" href="#%E6%97%B6%E9%97%B4%E5%A4%8D%E6%9D%82%E5%BA%A6"><span class="toc-number">2.7.4.5.1.</span> <span class="toc-text">时间复杂度</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#%E7%A9%BA%E9%97%B4%E5%A4%8D%E6%9D%82%E5%BA%A6"><span class="toc-number">2.7.4.5.2.</span> <span class="toc-text">空间复杂度</span></a></li></ol></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E4%BA%8C%E7%BB%B4%E6%95%B0%E7%BB%84"><span class="toc-number">2.7.4.6.</span> <span class="toc-text">二维数组</span></a><ol class="toc-child"><li class="toc-item toc-level-5"><a class="toc-link" href="#%E4%BA%8C%E7%BB%B4%E6%95%B0%E7%BB%84%E5%AE%9E%E7%8E%B0%E6%9D%A8%E8%BE%89%E4%B8%89%E8%A7%92"><span class="toc-number">2.7.4.6.1.</span> <span class="toc-text">二维数组实现杨辉三角</span></a></li></ol></li></ol></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#java-%E5%86%85%E5%AD%98%E6%A8%A1%E5%9E%8B%E5%88%9D%E8%AF%86"><span class="toc-number">2.8.</span> <span class="toc-text">java 内存模型初识</span></a></li></ol></li></ol></div></div></div></aside><main class="sidebar-translate" id="content"><div id="post"><article class="hty-card post-block" itemscope itemtype="https://schema.org/Article"><link itemprop="mainEntityOfPage" href="http://zilongzeng.gitee.io/web/web/2022/09/13/java%E5%88%9D%E7%BA%A7/"><span hidden itemprop="author" itemscope itemtype="https://schema.org/Person"><meta itemprop="name" content="zzl"><meta itemprop="description"></span><span hidden itemprop="publisher" itemscope itemtype="https://schema.org/Organization"><meta itemprop="name" content="zzl"></span><header class="post-header"><h1 class="post-title" itemprop="name headline">java初级</h1><div class="post-meta"><div class="post-time" style="display:inline-block"><span class="post-meta-item-icon"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-calendar-line"></use></svg></span> <time title="Created: 2022-09-13 16:09:37" itemprop="dateCreated datePublished" datetime="2022-09-13T16:09:37+08:00">2022-09-13</time></div><div class="post-classify"><span class="post-tag"><a class="tag-item" href="/web/tags/java/" style="--text-color:var(--hty-text-color)"><span class="post-meta-item-icon"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-price-tag-3-line"></use></svg></span><span class="tag-name">java</span></a></span></div></div></header><section class="post-body" itemprop="articleBody"><div class="post-content markdown-body" style="--smc-primary:#0078E7;"><h1 id="基础知识"><a href="#基础知识" class="headerlink" title="基础知识"></a>基础知识</h1><h2 id="计算机常识"><a href="#计算机常识" class="headerlink" title="计算机常识"></a>计算机常识</h2><ul>
<li>计算分为软件和硬件<ul>
<li>实物存在就是硬件</li>
<li>电脑开启后可以使用的就是软件</li>
</ul>
</li>
<li>常见系统：<ul>
<li>windows 系统</li>
<li>linux 系统</li>
<li>unix 系统</li>
<li>mac 系统</li>
<li>国产系统</li>
</ul>
</li>
<li>windows 系统将指令操作转化为更加便捷的方式，让用户使用<ul>
<li>直接使用图形化界面</li>
<li>可以通过命令</li>
</ul>
</li>
<li>常用的 DOS 命令<ul>
<li>开启命令窗口：win + r ，输入 cmd </li>
<li>进行位置移动：cd 文件路径<ul>
<li>cd .. ：移动至上一级目录</li>
<li>cd\ ：退回至盘符根路径</li>
<li>盘符移动：盘符名:</li>
</ul>
</li>
<li>查看当前文件夹中的文件列表：dir<ul>
<li>dir /a ：查看文件列表(包含隐藏文件)</li>
<li>dir /a &gt;&gt; E:\sc20220501java\dome.txt ，将查询结果输出至某个文件</li>
</ul>
</li>
<li>查看当前文件夹结构，以树状显示：tree<ul>
<li>tree /f ：显示文件夹结构及所有的文件内容，以树状显示</li>
</ul>
</li>
<li>创建文件夹： md 文件夹名<ul>
<li>md .\test\one\two 一次创建多个文件夹</li>
</ul>
</li>
<li>删除文件：del 文件名</li>
<li>删除文件夹：rd 文件夹名</li>
<li>清屏：cls</li>
<li>查看当前电脑 ip：inconfig</li>
<li>ping 对应的地址：ping ip地址</li>
<li>查询当前所有的程序、网络状态以及对应的程序：netstat -ano</li>
<li>查看当前文件内容：more 包含后缀的文件名</li>
<li>计算机控制<ul>
<li>shutdown /s ：关机</li>
<li>shutdown /r ：重启</li>
<li>shutdown /l ：注销</li>
<li>shutdown /s /t 300 ：定时关机，5分钟后关机</li>
<li>shutdown /a ：取消关机</li>
</ul>
</li>
<li>终止当前运行的指令：ctrl + c</li>
</ul>
</li>
</ul>
<h2 id="计算机的二进制"><a href="#计算机的二进制" class="headerlink" title="计算机的二进制"></a>计算机的二进制</h2><ul>
<li><p>不管是内存还是磁盘，都是以二进制进行存储。就像一个巨大的表格，里面存放着 0 和 1</p>
</li>
<li><p>存储单位：</p>
<ul>
<li>1 bit ：一位</li>
<li>1 Byte = 8 bit ，一个字节</li>
<li>1 KB = 1024B</li>
<li>1MB = 1024KB</li>
<li>1GB = 1024MB</li>
<li>1TB = 1024 GB</li>
</ul>
</li>
<li><p>常用的进制</p>
<ul>
<li>二进制<ul>
<li>逢二进一</li>
</ul>
</li>
<li>八进制<ul>
<li>逢八进一</li>
</ul>
</li>
<li>十进制(0,1,2,3…9)<ul>
<li>逢十进一</li>
</ul>
</li>
<li>十六进制(0,1,3,4,5,…a,b,c,d,e,f)<ul>
<li>逢十六进一</li>
</ul>
</li>
</ul>
</li>
</ul>
<h3 id="二进制的加减法"><a href="#二进制的加减法" class="headerlink" title="二进制的加减法"></a>二进制的加减法</h3><ul>
<li><p>二进制的加点法与十进制思路是一致的</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line">加法：</span><br><span class="line">	10100110</span><br><span class="line">+	00110100</span><br><span class="line">--------------</span><br><span class="line">    11011010</span><br><span class="line">	</span><br><span class="line"></span><br><span class="line">减法：</span><br><span class="line">	10100110</span><br><span class="line">-	01001011</span><br><span class="line">--------------</span><br><span class="line">    01011011</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br></pre></td></tr></table></figure></li>
<li><p>思考疑问题：</p>
<ul>
<li>二进制如何表示正负数？符合判断多长的二进制是一个数字？</li>
<li>十进制中使用 + 和 - 表示数字的正负值，但是这个两个符号在二进制中是没有的。正负号本质上就是一个大家都承认的符号，只要大家都遵循该约定他们才是正负号。在二进制中就可以既定一个规则表示正负号<ul>
<li>我们可以设置一个二进制的数字的首位表示正负数，1 表示为负数，0表示为正数。例如：0101 正数，1001 负数</li>
</ul>
</li>
<li>比如如果出现想要表示多个数字：1010 11 110101，表示为三个数字。但是计算机无法智能识别。那我们可以通过指定二进制数字的位数表示为一个数字，长度的范围必须为一个定值。<ul>
<li>比如说可以定位 8 位长度为一个数字例如：0000 1010、0000 0011、0011 0101</li>
<li>对应的：1000 0001 ：-1，0000 0001 ：1</li>
</ul>
</li>
</ul>
</li>
</ul>
<h3 id="二进制的原码，反码，补码"><a href="#二进制的原码，反码，补码" class="headerlink" title="二进制的原码，反码，补码"></a>二进制的原码，反码，补码</h3><ul>
<li><p>问题：二进制的 1(0000 0001) 加一个 -1(1000 0001) 会等于 0 吗？</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">二进制的 1 加 -1</span><br><span class="line"></span><br><span class="line">    0000 0001</span><br><span class="line">+   1000 0001</span><br><span class="line">--------------</span><br><span class="line">	1000 0010	</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br></pre></td></tr></table></figure></li>
<li><p>在进行二进制的计算式，因为负数有符号位概念，如果直接运算是得不到正确结果的。在使用之前需要将运算数字获取其补码，补码又需要通过反码获取</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br></pre></td><td class="code"><pre><span class="line">正数：原码，反码，补码都是它本身</span><br><span class="line">	  0000 0001</span><br><span class="line">	原码：0000 0001</span><br><span class="line">	反码：0000 0001</span><br><span class="line">	补码：0000 0001</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">负数：原码它本身，反码为除首位符号位的其他位的反值，补码为反码加 1</span><br><span class="line">	  1000 0001</span><br><span class="line">	原码：1000 0001</span><br><span class="line">	反码：1111 1110</span><br><span class="line">	补码：1111 1111</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">计算机中的存储只会存一个数的补码，计算也是通过其补码计算</span><br><span class="line"></span><br><span class="line">	0000 0001</span><br><span class="line">+	1111 1111</span><br><span class="line">---------------</span><br><span class="line">  1 0000 0000</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line">计算： 3 + -3</span><br><span class="line"></span><br><span class="line">3的原反补：</span><br><span class="line">	  0000 0011</span><br><span class="line">	原码：0000 0011</span><br><span class="line">	反码：0000 0011</span><br><span class="line">	补码：0000 0011</span><br><span class="line">-3的原反补</span><br><span class="line">	  1000 0011</span><br><span class="line">	原码：1000 0011</span><br><span class="line">	反码：1111 1100</span><br><span class="line">	补码：1111 1101</span><br><span class="line">	</span><br><span class="line">	0000 0011</span><br><span class="line">+	1111 1101</span><br><span class="line">---------------	</span><br><span class="line">  1 0000 0000</span><br><span class="line">  </span><br><span class="line">  </span><br><span class="line">  </span><br><span class="line">计算 0 + -0  </span><br><span class="line"></span><br><span class="line">0的原反补：</span><br><span class="line">	  0000 0000</span><br><span class="line">	原码：0000 0000</span><br><span class="line">	反码：0000 0000</span><br><span class="line">	补码：0000 0000</span><br><span class="line">-0的原反补</span><br><span class="line">	  1000 0000</span><br><span class="line">	原码：1000 0000</span><br><span class="line">	反码：1111 1111</span><br><span class="line">	补码：1 0000 0000</span><br><span class="line">	</span><br><span class="line">	0000 0000</span><br><span class="line">+ 1 0000 0000</span><br><span class="line">---------------	</span><br><span class="line">  1 0000 0000</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br></pre></td></tr></table></figure></li>
</ul>
<h3 id="二进制的逻辑运算"><a href="#二进制的逻辑运算" class="headerlink" title="二进制的逻辑运算"></a>二进制的逻辑运算</h3><ul>
<li>逻辑运算相比加减乘除对于计算机而言就是简单运算方式</li>
</ul>
<h4 id="与运算"><a href="#与运算" class="headerlink" title="与运算"></a>与运算</h4><ul>
<li><p>与运算符：&amp;</p>
</li>
<li><p>与运算就好比串联电路，都闭合才通，只要有一个断开则不通</p>
</li>
<li><p>计算机中的表达：1为闭合，0为断开</p>
<ul>
<li>1 &amp; 1 = 1</li>
<li>1 &amp; 0 = 0</li>
<li>0 &amp; 1 = 0</li>
<li>0 &amp; 0 = 0</li>
</ul>
</li>
<li><p>计算 5 &amp; 3</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line">5二进制为：0000 0101</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">3二进制为：0000 0011</span><br><span class="line">都为正数原反补一致</span><br><span class="line"></span><br><span class="line">	0000 0101</span><br><span class="line">&amp;	0000 0011</span><br><span class="line">-----------------</span><br><span class="line">    0000 0001</span><br><span class="line"></span><br><span class="line">最终结果转化为十进制为 1</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br></pre></td></tr></table></figure></li>
</ul>
<h4 id="或运算"><a href="#或运算" class="headerlink" title="或运算"></a>或运算</h4><ul>
<li><p>或运算符号：|</p>
</li>
<li><p>或运算就好比并联电路，都断开才不通，只要有一个闭合则通路</p>
</li>
<li><p>计算机中的表达：1为闭合，0为断开</p>
<ul>
<li>1 | 1 = 1</li>
<li>1 | 0 = 1</li>
<li>0 | 1 = 1</li>
<li>0 | 0 = 0</li>
</ul>
</li>
<li><p>计算 5 &amp; 3</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"></span><br><span class="line">5二进制为：0000 0101</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">3二进制为：0000 0011</span><br><span class="line">都为正数原反补一致</span><br><span class="line"></span><br><span class="line">	0000 0101</span><br><span class="line">|	0000 0011</span><br><span class="line">-----------------</span><br><span class="line">    0000 0111</span><br><span class="line"></span><br><span class="line">最终结果转化为十进制为 7</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br></pre></td></tr></table></figure></li>
</ul>
<h3 id="非运算"><a href="#非运算" class="headerlink" title="非运算"></a>非运算</h3><ul>
<li>非运算符号：!</li>
<li>非运算就好比会短路的电路，闭合电路灯不亮，断开电路灯亮</li>
<li>计算机中的表达：1为闭合，0为断开<ul>
<li>!1 = 0</li>
<li>!0 = 1</li>
</ul>
</li>
</ul>
<h1 id="java入门"><a href="#java入门" class="headerlink" title="java入门"></a>java入门</h1><h2 id="java概述"><a href="#java概述" class="headerlink" title="java概述"></a>java概述</h2><h3 id="什么是-java"><a href="#什么是-java" class="headerlink" title="什么是 java"></a>什么是 java</h3><ul>
<li>Java是一门面向对象的编程语言</li>
<li>java是一种软件开发、运行、部署的平台</li>
<li>总的来说，java既是一门面向对象的编程语言，也是一个平台</li>
</ul>
<h3 id="java-能做什么"><a href="#java-能做什么" class="headerlink" title="java 能做什么"></a>java 能做什么</h3><ul>
<li>java能做的事情有很多，涉及到了方方面面的领域</li>
<li>桌面级应用(javaSE)</li>
<li>企业级应用(javaEE)</li>
<li>嵌入式设备(javaME)</li>
<li>大数据领域产品</li>
</ul>
<h3 id="java-的历史"><a href="#java-的历史" class="headerlink" title="java 的历史"></a>java 的历史</h3><ul>
<li>在1991年成立了一个称为Green的项目小组，OAK诞生</li>
<li>1995年5月23日，OAK改名为java正式发布</li>
<li>2004年9月30日，java5发布</li>
<li>2014年，甲骨文公司发布了Java8正式版</li>
</ul>
<h3 id="java-的语言特点"><a href="#java-的语言特点" class="headerlink" title="java 的语言特点"></a>java 的语言特点</h3><ul>
<li>简单性</li>
<li>面向对象</li>
<li>分布性</li>
<li>编译和解释性</li>
<li>稳健性</li>
<li>安全性</li>
<li>可移植性</li>
<li>高性能</li>
<li>多线程性</li>
<li>动态性</li>
</ul>
<h2 id="java-的基础使用"><a href="#java-的基础使用" class="headerlink" title="java 的基础使用"></a>java 的基础使用</h2><ul>
<li>首先需要安装开发环境（JDK）<ul>
<li>JDK<ul>
<li>JDK 是 java 开发环境，JDK = jre + java 开发工具(javadoc,javac,javap)</li>
</ul>
</li>
<li>JRE<ul>
<li>JRE 是 java 的运行环境，JRE = JVM + java核心类库</li>
</ul>
</li>
<li>JDK、JRE、JVM 之间的关系<ul>
<li>JDK = JRE + java 开发工具(javadoc,javac,javap)</li>
<li>JRE = JVM + java核心类库</li>
<li>JVM 就是java 的虚拟机，编译程序(解释)为计算机可执行的文件，也是跨平台的核心</li>
<li>如果只想运行 .class 文件只需要 JRE 就好了</li>
</ul>
</li>
</ul>
</li>
<li>程序开发的步骤：<ul>
<li>编写 .java 文件(程序员) ==》编译 .class 文件(编译器) ==》运行(jvm)</li>
<li>javac.exe 编译器</li>
<li>java.exe 解释器</li>
</ul>
</li>
</ul>
<h3 id="编写第一个-HelloWorld-java-文件"><a href="#编写第一个-HelloWorld-java-文件" class="headerlink" title="编写第一个 HelloWorld.java 文件"></a>编写第一个 HelloWorld.java 文件</h3><ul>
<li><p>打开 notepad++ 在一个新的 HelloWorld.java 文件中输入以下内容</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">HelloWorld</span></span>&#123;</span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span></span>&#123;</span><br><span class="line">		System.out.println(<span class="string">&quot;Hello World!&quot;</span>);</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure></li>
<li><p>完成后通过 javac 进行编译</p>
<ul>
<li><p>编译过程就是将 高级的编程语言转换为二进制</p>
</li>
<li><p>编译需要指定 javac 文件工具</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 找到本地的 javac 所在路径</span></span><br><span class="line">javac:</span><br><span class="line"><span class="string">&quot;C:\Program Files\jdk\bin\javac&quot;</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// 在 cmd 命名窗口中输入指令</span></span><br><span class="line">cmd:<span class="string">&quot;C:\Program Files\jdk\bin\javac&quot;</span> HelloWorld.java</span><br><span class="line"></span><br></pre></td></tr></table></figure></li>
</ul>
</li>
<li><p>编译成功后会生成一个 .class 文件，这个文件就是二进制文件将来他要加载到内存运行中</p>
</li>
<li><p>通过 java.exe 解释器对 .class 二进制文件进行解释，最终执行</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 找到本地的 javac 所在路径</span></span><br><span class="line">java.exe:</span><br><span class="line"><span class="string">&quot;C:\Program Files\jdk\bin\java&quot;</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// 在 cmd 命名窗口中输入指令</span></span><br><span class="line">cmd:<span class="string">&quot;C:\Program Files\jdk\bin\java&quot;</span> HelloWorld</span><br><span class="line"></span><br></pre></td></tr></table></figure></li>
<li><p>配置了环境变量后，可以省略路径开头</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 配置环境变量后可以简化</span></span><br><span class="line">cmd:javac HelloWorld.java</span><br><span class="line">cmd:java HelloWorld</span><br><span class="line"></span><br></pre></td></tr></table></figure></li>
<li><p>总结：</p>
<ul>
<li>编译 java 需要使用 javac</li>
<li>java 程序运行需要 java.exe</li>
</ul>
</li>
</ul>
<h2 id="java-中的标识符和注释"><a href="#java-中的标识符和注释" class="headerlink" title="java 中的标识符和注释"></a>java 中的标识符和注释</h2><h3 id="标识符"><a href="#标识符" class="headerlink" title="标识符"></a>标识符</h3><ul>
<li>标识符：指在程序中，我们自己定义的内容。比如：类名，方法名，变量名等等，都是标识符。标识符的要求可以分为命名规范和规则<ul>
<li>命名规则(必须遵守)：<ul>
<li>可以包含数字，字母(区分大小写)，_和$</li>
<li>不能以数字开头</li>
<li>不能是关键字</li>
</ul>
</li>
<li>命名规范(行业要求)：<ul>
<li>类名：首字母大写，后面每个单词的首字母都大写(大驼峰式)。注意！public 修饰的类名必须与文件名保持一致</li>
<li>变量名：首字母小写，后面每个单词的首字母都大写(小驼峰式)</li>
<li>方法名：与变量名一致</li>
</ul>
</li>
</ul>
</li>
</ul>
<h3 id="注释"><a href="#注释" class="headerlink" title="注释"></a>注释</h3><ul>
<li><p>在编写代码时候需要对代码进行说明，就需要使用到注释，在java程序运行中会忽略注释</p>
</li>
<li><p>java中常用的注释有三种：</p>
<ul>
<li><p>单行注释</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 单行注释 </span></span><br><span class="line"></span><br></pre></td></tr></table></figure></li>
<li><p>多行注释</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">	多行注释，以 /* 开头，以 */</span> 结尾</span><br><span class="line">*/</span><br><span class="line"></span><br></pre></td></tr></table></figure></li>
<li><p>文档注释</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 这是文档注释，以 /** 开头，以 /结尾</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"></span><br></pre></td></tr></table></figure></li>
</ul>
</li>
<li><p>一般而言，对单行文本说明使用单行注释，对多行文本说明使用多行注释。对 类、方法 说明使用文档注释</p>
</li>
<li><p>对第一个 HelloWorld 进行说明</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"><span class="comment">// public class 类名</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">HelloWorld</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 程序的入口 （main方法，主方法，主函数）</span></span><br><span class="line">    <span class="comment">// 访问修饰 静态的 返回值类型 方法名(参数列表)</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span></span>&#123;</span><br><span class="line">        <span class="comment">// 需要执行的 java 代码</span></span><br><span class="line">        <span class="comment">// System.out.println(需要打印的内容);</span></span><br><span class="line">        <span class="comment">// 在控制台打印语句</span></span><br><span class="line">        System.out.println(<span class="string">&quot;你好世界&quot;</span>);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">================================</span><br><span class="line">    </span><br><span class="line"><span class="keyword">public</span>：访问修饰符</span><br><span class="line"><span class="class"><span class="keyword">class</span>：类</span></span><br><span class="line"><span class="class"><span class="title">static</span>：静态的</span></span><br><span class="line"><span class="class"><span class="title">void</span>：返回值类型</span></span><br><span class="line"><span class="class"><span class="title">main</span>：方法名</span></span><br><span class="line"><span class="class">    </span></span><br><span class="line"><span class="class"></span></span><br><span class="line"><span class="class"></span></span><br></pre></td></tr></table></figure></li>
</ul>
<h2 id="java-中的变量和运算符号"><a href="#java-中的变量和运算符号" class="headerlink" title="java 中的变量和运算符号"></a>java 中的变量和运算符号</h2><ul>
<li><p>在 java 中表示 <strong>一个可变内容的量称之为变量</strong> ，而 <strong>运行期间不可改变的量称之为常量</strong> ，并且 java 是一种强类型语言，所以在声明变量的时候需要指定其数据类型</p>
</li>
<li><p>变量声明格式</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 变量的声明格式</span></span><br><span class="line">数据类型 变量名 = 值;</span><br><span class="line"></span><br></pre></td></tr></table></figure></li>
</ul>
<h3 id="java-中的数据类型"><a href="#java-中的数据类型" class="headerlink" title="java 中的数据类型"></a>java 中的数据类型</h3><ul>
<li><p>java 中有八大基本数据类型</p>
<table>
<thead>
<tr>
<th>数据类型</th>
<th>类别</th>
<th>占用内存</th>
<th>取值范围</th>
</tr>
</thead>
<tbody><tr>
<td>byte</td>
<td>整型</td>
<td>1个字节(8位)</td>
<td>-128~127</td>
</tr>
<tr>
<td>short</td>
<td>整型</td>
<td>2个字节(16位)</td>
<td>-32768~32767</td>
</tr>
<tr>
<td>int</td>
<td>整型</td>
<td>4个字节(32位)</td>
<td>-2147483648~2147483647</td>
</tr>
<tr>
<td>long</td>
<td>整型</td>
<td>8个字节(64位)</td>
<td>-9223372036854774808~9223372036854774807</td>
</tr>
<tr>
<td>float</td>
<td>浮点型</td>
<td>4个字节(32位)</td>
<td>3.402823e+38 ~ 1.401298e-45</td>
</tr>
<tr>
<td>double</td>
<td>浮点型</td>
<td>8个字节(64位)</td>
<td>1.797693e+308~ 4.9000000e-324</td>
</tr>
<tr>
<td>char</td>
<td>字符型</td>
<td>2个字节(16位)</td>
<td>65535（看字符编码）</td>
</tr>
<tr>
<td>boolean</td>
<td>布尔型</td>
<td>1个字节(8位)</td>
<td>true 或 false</td>
</tr>
</tbody></table>
</li>
<li><p>相同类别的数据，不同的大小是为了更加灵活的利用内存空间</p>
</li>
</ul>
<h4 id="整型"><a href="#整型" class="headerlink" title="整型"></a>整型</h4><ul>
<li><p>整型包含了 byte、short、int、long 四种</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.seecen.day01;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 整数类型</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">IntegerType</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span></span>&#123;</span><br><span class="line">        <span class="comment">// 声明一个 byte 类型</span></span><br><span class="line">        <span class="keyword">byte</span> a = <span class="number">7</span>;</span><br><span class="line">        <span class="comment">// 声明一个 short 类型</span></span><br><span class="line">        <span class="keyword">short</span> b = <span class="number">17</span>;</span><br><span class="line">        <span class="comment">// 声明一个 int 类型</span></span><br><span class="line">        <span class="keyword">int</span> c = <span class="number">37</span>;</span><br><span class="line">        <span class="comment">// 声明一个 long 类型，推荐结尾添加L</span></span><br><span class="line">        <span class="keyword">long</span> d = <span class="number">71267128736L</span>;</span><br><span class="line">        System.out.println(a);</span><br><span class="line">        System.out.println(b);</span><br><span class="line">        System.out.println(c);</span><br><span class="line">        System.out.println(d);</span><br><span class="line"></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure></li>
</ul>
<h5 id="整型的取值"><a href="#整型的取值" class="headerlink" title="整型的取值"></a>整型的取值</h5><ul>
<li><p>不同类型有不同的取值范围，计算取值范围的思路一致</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br></pre></td><td class="code"><pre><span class="line">计算 byte 的取值范围</span><br><span class="line">byte 占 1个字节 8 位</span><br><span class="line"></span><br><span class="line">8位二进制的范围</span><br><span class="line">0000 0000 - 1111 1111 (0-255)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">因为首位为符号位所以：</span><br><span class="line">byte 的最大值原码：0111 1111</span><br><span class="line">byte 的最大值反码：0111 1111</span><br><span class="line">byte 的最大值补码：0111 1111</span><br><span class="line"></span><br><span class="line">byte 的最小值原码：1111 1111</span><br><span class="line">byte 的最小值反码：1000 0000</span><br><span class="line">byte 的最小值补码：1000 0001</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">转化为十进制：</span><br><span class="line">最大值为 127</span><br><span class="line">最小值为 -127</span><br><span class="line"></span><br><span class="line">如果正负数都进行不停的减一，就会剩下一个 1000 0000 根据二进制转化应该是 -0</span><br><span class="line"></span><br><span class="line">可是这个 -0 没有意义那么这个数就是多出来的</span><br><span class="line">-0的补码 1 0000 0000 </span><br><span class="line">就成为了一个最小负数 -128</span><br><span class="line"></span><br><span class="line">byte 的最大值：0111 1111</span><br><span class="line">byte 的最小值：1000 00008</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br></pre></td></tr></table></figure></li>
</ul>
<h4 id="浮点型"><a href="#浮点型" class="headerlink" title="浮点型"></a>浮点型</h4><ul>
<li><p>浮点型包含 float、double</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.seecen.day01;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@program</span>: javaSE</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@description</span>:</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@author</span>: lan</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@create</span>: 2022-07-06 17:05</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@version</span>:1.0</span></span><br><span class="line"><span class="comment"> **/</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">FloatingPointType</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span></span>&#123;</span><br><span class="line">        <span class="comment">// 声明一个 float ，需要添加 F 结尾</span></span><br><span class="line">        <span class="keyword">float</span> a = <span class="number">123.32F</span>;</span><br><span class="line">        <span class="comment">// 声明一个 double</span></span><br><span class="line">        <span class="keyword">double</span> b = <span class="number">76534.123</span>;</span><br><span class="line">        System.out.println(a);</span><br><span class="line">        System.out.println(b);</span><br><span class="line"></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"></span><br></pre></td></tr></table></figure></li>
</ul>
<h4 id="字符型"><a href="#字符型" class="headerlink" title="字符型"></a>字符型</h4><ul>
<li><p>字符型包含 char 一种</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.seecen.day01;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> java.nio.charset.Charset;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@program</span>: javaSE</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@description</span>:</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@author</span>: lan</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@create</span>: 2022-07-06 17:08</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@version</span>:1.0</span></span><br><span class="line"><span class="comment"> **/</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">CharacterType</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span></span>&#123;</span><br><span class="line">        <span class="comment">// 声明一个 char 类型</span></span><br><span class="line">        <span class="keyword">char</span> a = <span class="string">&#x27;q&#x27;</span>;</span><br><span class="line">        System.out.println(a);</span><br><span class="line">        <span class="comment">// 查看当前的字符集</span></span><br><span class="line">        System.out.println(Charset.defaultCharset());</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"></span><br></pre></td></tr></table></figure></li>
</ul>
<h4 id="布尔型"><a href="#布尔型" class="headerlink" title="布尔型"></a>布尔型</h4><ul>
<li><p>布尔类型包含 boolean 一种</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.seecen.day01;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@program</span>: javaSE</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@description</span>:</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@author</span>: lan</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@create</span>: 2022-07-06 17:21</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@version</span>:1.0</span></span><br><span class="line"><span class="comment"> **/</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">BooleanType</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span></span>&#123;</span><br><span class="line">        <span class="comment">// 声明一个 boolean 类型</span></span><br><span class="line">        <span class="keyword">boolean</span> a = <span class="keyword">true</span>;</span><br><span class="line">        System.out.println(a);</span><br><span class="line"></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure></li>
</ul>
<h4 id="基本数据类型总结"><a href="#基本数据类型总结" class="headerlink" title="基本数据类型总结"></a>基本数据类型总结</h4><ul>
<li><p>在同一个代码块(一对大括号)中不能出现同名变量</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">&#123;</span><br><span class="line">    <span class="keyword">int</span> a = <span class="number">5</span>;</span><br><span class="line">    <span class="comment">// 错误，不能出现同名变量</span></span><br><span class="line">    <span class="keyword">int</span> a = <span class="number">8</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure></li>
<li><p>整型常量默认为 int 类型，浮点型常量默认为 double 类型</p>
</li>
<li><p>代码块中定义一个未赋值的变量，在使用之前必须赋值</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">&#123;</span><br><span class="line">    <span class="keyword">int</span> a;</span><br><span class="line">    <span class="comment">// 错误，必须赋值后才能使用</span></span><br><span class="line">    System.out.println(a);</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure></li>
</ul>
<h3 id="java-中的运算符号"><a href="#java-中的运算符号" class="headerlink" title="java 中的运算符号"></a>java 中的运算符号</h3><h4 id="算数运算符"><a href="#算数运算符" class="headerlink" title="算数运算符"></a>算数运算符</h4><ul>
<li><p>基础的运算符：+、-、*、/</p>
</li>
<li><p>特殊的运算符：%、++、–</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Demo01</span> </span>&#123;</span><br><span class="line">    <span class="comment">// PSVM</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">        <span class="comment">// 基础运算符：+、-、*、/</span></span><br><span class="line">        <span class="comment">// 1，常量之间遵循四则运算规则</span></span><br><span class="line">        System.out.println(<span class="number">25</span> + <span class="number">68</span>);</span><br><span class="line">        <span class="comment">// 2，如果小于 int 的类型进行运算，返回值会为 int</span></span><br><span class="line">        <span class="keyword">short</span> num1 = <span class="number">21</span>;</span><br><span class="line">        <span class="keyword">byte</span> num2 = <span class="number">32</span>;</span><br><span class="line">        <span class="keyword">int</span> num3 = num1 + num2;</span><br><span class="line">        System.out.println(num3);</span><br><span class="line">        <span class="comment">// 3，变量的运算中运算结果，返回大类型</span></span><br><span class="line">        <span class="keyword">byte</span> num4 = <span class="number">26</span>;</span><br><span class="line">        <span class="keyword">long</span> num5 = <span class="number">17L</span>;</span><br><span class="line">        <span class="keyword">long</span> num6 = num4 + num5;</span><br><span class="line">        System.out.println(num6);</span><br><span class="line">        <span class="comment">// 4，如果整数与浮点数进行运算，返回必定是浮点数</span></span><br><span class="line">        <span class="keyword">float</span> num7 = <span class="number">29.0F</span>;</span><br><span class="line">        <span class="keyword">long</span> num8 = <span class="number">52L</span>;</span><br><span class="line">        <span class="keyword">float</span> num9 = num7 + num8;</span><br><span class="line">        System.out.println(num9);</span><br><span class="line"></span><br><span class="line">        System.out.println(<span class="string">&quot;================================&quot;</span>);</span><br><span class="line">        <span class="comment">// 特殊运算符：%、++、--</span></span><br><span class="line">        <span class="comment">// 1， % 为取余符号，可以计算除两个数相除的余数</span></span><br><span class="line">        System.out.println(<span class="number">54</span> % <span class="number">7</span>);</span><br><span class="line">        <span class="comment">// 2，++ 和 -- 的使用规则一致，一个是自增 1，一个是自减 1</span></span><br><span class="line">        <span class="keyword">int</span> num10 = <span class="number">87</span>;</span><br><span class="line">        <span class="comment">// ++/-- 在后，先操作，再赋值运算</span></span><br><span class="line">        System.out.println(num10--);</span><br><span class="line">        <span class="comment">// ++/-- 在前，先赋值运算，再操作</span></span><br><span class="line">        System.out.println(++num10);</span><br><span class="line">        <span class="comment">// ++/-- 不能给常量使用</span></span><br><span class="line"><span class="comment">//        System.out.println(8++);</span></span><br><span class="line"></span><br><span class="line">        System.out.println(<span class="string">&quot;===================================&quot;</span>);</span><br><span class="line">        <span class="comment">// char 进行运算时会根据 ASCII 转换为 int 类型</span></span><br><span class="line">        <span class="keyword">char</span> c1 = <span class="string">&#x27;a&#x27;</span>;</span><br><span class="line">        <span class="keyword">char</span> c2 = <span class="string">&#x27;b&#x27;</span>;</span><br><span class="line">        System.out.println(c1 + c2);</span><br><span class="line">        System.out.println(++c1);</span><br><span class="line">        <span class="comment">// 思考：如果 char 使用 += 超过了范围或怎么样？</span></span><br><span class="line">        System.out.println(c1+=<span class="number">215</span>);</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure></li>
</ul>
<h4 id="赋值运算符"><a href="#赋值运算符" class="headerlink" title="赋值运算符"></a>赋值运算符</h4><ul>
<li><p>基础的赋值运算：=</p>
</li>
<li><p>特殊的赋值运算：+=、-=、*=、/=</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Demo02</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">        <span class="comment">// 基础的赋值运算符号：=</span></span><br><span class="line">        <span class="keyword">int</span> num1 = <span class="number">41</span>;</span><br><span class="line">        System.out.println(num1);</span><br><span class="line">        System.out.println(<span class="string">&quot;=====================&quot;</span>);</span><br><span class="line"></span><br><span class="line">        <span class="comment">// 特殊运算符：+=、-=、*=、/=</span></span><br><span class="line">        <span class="comment">// 1，使用特殊运算符进行运算赋值时</span></span><br><span class="line">        <span class="comment">// 最终的结果不论大小，都会强制转化为符号左侧的类型</span></span><br><span class="line">        <span class="keyword">byte</span> num2 = <span class="number">34</span>;</span><br><span class="line">        <span class="keyword">short</span> num3 = <span class="number">120</span>;</span><br><span class="line"><span class="comment">//        num2 = num2 + num3;</span></span><br><span class="line">        num2 += num3;</span><br><span class="line">        System.out.println(num2);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"></span><br></pre></td></tr></table></figure></li>
</ul>
<h4 id="比较运算符"><a href="#比较运算符" class="headerlink" title="比较运算符"></a>比较运算符</h4><ul>
<li><p>比较运算符：&gt;、&lt;、==、&gt;=、&lt;=、!=</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Demo03</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">        <span class="comment">// 比较运算符：&gt;、&lt;、==、&gt;=、&lt;=、!=</span></span><br><span class="line">        <span class="comment">// 1，比较运算符只会返回布尔类型</span></span><br><span class="line">        <span class="keyword">byte</span> num1 = <span class="number">34</span>;</span><br><span class="line">        <span class="keyword">short</span> num2 = <span class="number">25</span>;</span><br><span class="line">        System.out.println(num1 &gt; num2);</span><br><span class="line">        <span class="comment">// 2，如果比较中出现了 long 类型，会将非 long 类型的变量都转化为 long 类型</span></span><br><span class="line">        <span class="keyword">long</span> num3 = <span class="number">65</span>;</span><br><span class="line">        <span class="keyword">short</span> num4 = <span class="number">12</span>;</span><br><span class="line">        System.out.println(num3 &gt; num4);</span><br><span class="line">        <span class="comment">// 3，比较中出现了浮点类型，会将非浮点的类型转化为浮点类型</span></span><br><span class="line">        <span class="keyword">float</span> num5 = <span class="number">8.9F</span>;</span><br><span class="line">        <span class="keyword">long</span> num6 = <span class="number">123</span>;</span><br><span class="line">        System.out.println(num5 &gt; num6);</span><br><span class="line"></span><br><span class="line">        <span class="comment">// 4，如果进区间判断必须分开写</span></span><br><span class="line">        <span class="comment">// 10 &lt; a &lt; 20 ==&gt; 10 &lt; a &amp; a &lt;20</span></span><br><span class="line">        System.out.println(<span class="number">10</span> &lt; num6 &amp; num6 &lt; <span class="number">20</span>);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"></span><br></pre></td></tr></table></figure></li>
</ul>
<h4 id="逻辑运算符"><a href="#逻辑运算符" class="headerlink" title="逻辑运算符"></a>逻辑运算符</h4><ul>
<li><p>逻辑运算符：与&amp;&amp;、或||、非!</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Demo04</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">        <span class="comment">// 逻辑运算符：与&amp;&amp;、或||、非!</span></span><br><span class="line">        <span class="comment">// 1，逻辑运算符只能返回布尔类型</span></span><br><span class="line">        System.out.println(!(<span class="number">4</span> &gt; <span class="number">3</span>));</span><br><span class="line">        <span class="comment">// 2，使用 双&amp; 或 双| 都有断路的效果</span></span><br><span class="line">        <span class="comment">// || ：如果左侧为 true ，右侧不执行</span></span><br><span class="line">        System.out.println(<span class="number">3</span> &gt; <span class="number">1</span> | <span class="number">6</span> != <span class="number">8</span>);</span><br><span class="line">        System.out.println(<span class="number">3</span> &gt; <span class="number">1</span> || <span class="number">6</span> != <span class="number">8</span>);</span><br><span class="line">        <span class="comment">// &amp;&amp; ：如果左侧为 false ，右侧不执行</span></span><br><span class="line">        System.out.println(<span class="number">3</span> &lt; <span class="number">1</span> &amp; <span class="number">6</span> != <span class="number">8</span>);</span><br><span class="line">        System.out.println(<span class="number">3</span> &lt; <span class="number">1</span> &amp;&amp; <span class="number">6</span> != <span class="number">8</span>);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"></span><br></pre></td></tr></table></figure></li>
</ul>
<h4 id="三元运算符"><a href="#三元运算符" class="headerlink" title="三元运算符"></a>三元运算符</h4><ul>
<li><p>三元运算符：判断表达式？操作1:操作2</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Demo05</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">        <span class="comment">// 三元运算符：判断表达式？操作1:操作2</span></span><br><span class="line">        <span class="keyword">int</span> a = <span class="number">100</span>;</span><br><span class="line">        <span class="keyword">byte</span> b = <span class="number">20</span>;</span><br><span class="line">        <span class="comment">// 1，三元运算返回 true 时执行操作1，返回 false 时执行操作2</span></span><br><span class="line">        System.out.println(<span class="keyword">false</span> ? <span class="number">1</span> : <span class="number">2</span>);</span><br><span class="line"></span><br><span class="line">        <span class="comment">// 2，如果操作1与操作2返回类型相同，则返回的类型就是该类型</span></span><br><span class="line">        <span class="keyword">int</span> num1 = -<span class="number">1</span>;</span><br><span class="line">        num1 = (<span class="keyword">false</span> ? a : <span class="number">300</span>);</span><br><span class="line">        System.out.println(num1);</span><br><span class="line"></span><br><span class="line">        <span class="comment">// 3，如果有类型为 byte/short/char，并且另一个类型为 int</span></span><br><span class="line">        <span class="comment">// 结果没有超出 byte 范围，返回 byte/short/char</span></span><br><span class="line">        <span class="comment">// 如果结果超出则返回 int</span></span><br><span class="line">        <span class="keyword">byte</span> num2 = -<span class="number">1</span>;</span><br><span class="line">        num2 = (<span class="keyword">false</span> ? b : <span class="number">127</span>);</span><br><span class="line">        System.out.println(num2);</span><br><span class="line"></span><br><span class="line">        <span class="comment">// 4，不同类型进行三元运算最终结果会进行类型提升</span></span><br><span class="line">        <span class="keyword">long</span> num3 = -<span class="number">1</span>;</span><br><span class="line">        num3 = (<span class="keyword">true</span> ? a : <span class="number">230L</span>);</span><br><span class="line">        System.out.println(num3);</span><br><span class="line"></span><br><span class="line">        <span class="comment">// 5，如果有浮点型进行运算，必须返回浮点型</span></span><br><span class="line">        <span class="keyword">float</span> num4 = -<span class="number">1</span>;</span><br><span class="line">        num4 = (<span class="keyword">true</span> ? a : <span class="number">34F</span>);</span><br><span class="line">        System.out.println(num4);</span><br><span class="line"></span><br><span class="line">        <span class="comment">// 6，在操作中可以进行其他相关赋值</span></span><br><span class="line">        System.out.println(<span class="keyword">false</span> ? b : (a = <span class="number">18</span>));</span><br><span class="line">        System.out.println(a);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"></span><br></pre></td></tr></table></figure></li>
</ul>
<h4 id="位运算符"><a href="#位运算符" class="headerlink" title="位运算符"></a>位运算符</h4><ul>
<li><p>java 中支持六种位运算</p>
<ul>
<li>按位与&amp;、按位或|、按位非~、按位亦或^</li>
<li>左移 &lt;&lt;、右移&gt;&gt; / 无符号右移&gt;&gt;&gt;</li>
<li>注意！按位运算计算的都是补码！</li>
</ul>
</li>
<li><p>代码展示</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Demo06</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">        <span class="comment">// 注意！按位运算计算的都是补码！</span></span><br><span class="line">        <span class="comment">// 如果出现单个逻辑符号 &amp; 和 | 时将会进行按位运算</span></span><br><span class="line">        <span class="comment">// 按位运算或| ：同为0返回0，其他都为1</span></span><br><span class="line">        System.out.println(<span class="number">5</span> | <span class="number">3</span>);</span><br><span class="line">        <span class="comment">/*</span></span><br><span class="line"><span class="comment">        * -5原码：1000 0101</span></span><br><span class="line"><span class="comment">        * -5反码：1111 1010</span></span><br><span class="line"><span class="comment">        * -5补码：1111 1011</span></span><br><span class="line"><span class="comment">        *</span></span><br><span class="line"><span class="comment">        *   1111 1011</span></span><br><span class="line"><span class="comment">        *   0000 0011</span></span><br><span class="line"><span class="comment">        *   1111 1011</span></span><br><span class="line"><span class="comment">        *</span></span><br><span class="line"><span class="comment">        * 补码：1111 1011</span></span><br><span class="line"><span class="comment">        * 反码：1111 1010</span></span><br><span class="line"><span class="comment">        * 原码：1000 0101</span></span><br><span class="line"><span class="comment">        *</span></span><br><span class="line"><span class="comment">        * */</span></span><br><span class="line">        System.out.println(-<span class="number">5</span> | <span class="number">3</span>);</span><br><span class="line"></span><br><span class="line">        <span class="comment">// 按位 &amp; 运算：同为1返回1，其他返回0</span></span><br><span class="line">        <span class="comment">// 1101 &amp; 0111 ==》 0101 也就是 5</span></span><br><span class="line">        System.out.println(<span class="number">13</span> &amp; <span class="number">7</span>);</span><br><span class="line">        <span class="comment">/*</span></span><br><span class="line"><span class="comment">         * -13原码：1000 1101</span></span><br><span class="line"><span class="comment">         * -13反码：1111 0010</span></span><br><span class="line"><span class="comment">         * -13补码：1111 0011</span></span><br><span class="line"><span class="comment">         *</span></span><br><span class="line"><span class="comment">         *   1111 0011</span></span><br><span class="line"><span class="comment">         *   0000 0111</span></span><br><span class="line"><span class="comment">         *   0000 0011</span></span><br><span class="line"><span class="comment">         *</span></span><br><span class="line"><span class="comment">         * 原码：0000 0011</span></span><br><span class="line"><span class="comment">         *</span></span><br><span class="line"><span class="comment">         * */</span></span><br><span class="line">        System.out.println(-<span class="number">13</span> &amp; <span class="number">7</span>);</span><br><span class="line"></span><br><span class="line">        <span class="comment">// 按位亦或^：相同为0，不同为1</span></span><br><span class="line">        <span class="comment">// 1011 ^ 0011 ==》 1000 就是 8</span></span><br><span class="line">        System.out.println(<span class="number">11</span> ^ <span class="number">3</span>);</span><br><span class="line"></span><br><span class="line">        <span class="comment">/*</span></span><br><span class="line"><span class="comment">         * -11原码：1000 1011</span></span><br><span class="line"><span class="comment">         * -11反码：1111 0100</span></span><br><span class="line"><span class="comment">         * -11补码：1111 0101</span></span><br><span class="line"><span class="comment">         *</span></span><br><span class="line"><span class="comment">         *   1111 0101</span></span><br><span class="line"><span class="comment">         *   0000 0011</span></span><br><span class="line"><span class="comment">         *   1111 0110</span></span><br><span class="line"><span class="comment">         *</span></span><br><span class="line"><span class="comment">         * 补码：1111 0110</span></span><br><span class="line"><span class="comment">         * 反码：1111 0101</span></span><br><span class="line"><span class="comment">         * 原码：1000 1010</span></span><br><span class="line"><span class="comment">         *</span></span><br><span class="line"><span class="comment">         * */</span></span><br><span class="line">        System.out.println(-<span class="number">11</span> ^ <span class="number">3</span>);</span><br><span class="line"></span><br><span class="line">        <span class="comment">// 按位非~：1为0，0为1</span></span><br><span class="line">        <span class="comment">/*</span></span><br><span class="line"><span class="comment">         * 0000 0100</span></span><br><span class="line"><span class="comment">         * 1111 1011</span></span><br><span class="line"><span class="comment">         *</span></span><br><span class="line"><span class="comment">         * 补码：1111 1011</span></span><br><span class="line"><span class="comment">         * 反码：1111 1010</span></span><br><span class="line"><span class="comment">         * 原码：1000 0101</span></span><br><span class="line"><span class="comment">         *</span></span><br><span class="line"><span class="comment">         * */</span></span><br><span class="line">        System.out.println(~<span class="number">4</span>);</span><br><span class="line"></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br></pre></td></tr></table></figure></li>
<li><p>偏移运算</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Demo07</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">        <span class="comment">// 左移 &lt;&lt;：将二进制向左移动指定的位数，低位补零，符号位同时移动</span></span><br><span class="line">        <span class="comment">// 0000 1101 向左移动3位 0110 1000 就是 104，13*2^3</span></span><br><span class="line">        System.out.println(<span class="number">13</span> &lt;&lt; <span class="number">3</span>);</span><br><span class="line">        <span class="comment">/*</span></span><br><span class="line"><span class="comment">        * 原码：1000 1101</span></span><br><span class="line"><span class="comment">        * 反码：1111 0010</span></span><br><span class="line"><span class="comment">        * 补码：1111 0011</span></span><br><span class="line"><span class="comment">        *</span></span><br><span class="line"><span class="comment">        * 1111 0011 向左移动3位 1001 1000</span></span><br><span class="line"><span class="comment">        *</span></span><br><span class="line"><span class="comment">        * 补码：1001 1000</span></span><br><span class="line"><span class="comment">        * 反码：1001 0111</span></span><br><span class="line"><span class="comment">        * 原码：1110 1000</span></span><br><span class="line"><span class="comment">        * */</span></span><br><span class="line">        System.out.println(-<span class="number">13</span> &lt;&lt; <span class="number">3</span>);</span><br><span class="line"></span><br><span class="line">        <span class="comment">// 右移&gt;&gt;：将二进制向右移动指定的位数，符号位同时移动</span></span><br><span class="line">        <span class="comment">// 正数：高位补0，负数：高位补1</span></span><br><span class="line">        <span class="comment">/*</span></span><br><span class="line"><span class="comment">        *</span></span><br><span class="line"><span class="comment">        * 原码：1000 1101</span></span><br><span class="line"><span class="comment">        * 反码：1111 0010</span></span><br><span class="line"><span class="comment">        * 补码：1111 0011</span></span><br><span class="line"><span class="comment">        *</span></span><br><span class="line"><span class="comment">        * 1111 0011 向右移动2位，负数高位补1</span></span><br><span class="line"><span class="comment">        *</span></span><br><span class="line"><span class="comment">        * 补码：1111 1100</span></span><br><span class="line"><span class="comment">        * 反码：1111 1011</span></span><br><span class="line"><span class="comment">        * 原码：1000 0100</span></span><br><span class="line"><span class="comment">        *</span></span><br><span class="line"><span class="comment">        * 值为 -4</span></span><br><span class="line"><span class="comment">        *</span></span><br><span class="line"><span class="comment">        * */</span></span><br><span class="line">        System.out.println(-<span class="number">13</span> &gt;&gt; <span class="number">2</span>);</span><br><span class="line"></span><br><span class="line">        <span class="comment">// 无符号右移&gt;&gt;&gt;：将二进制向右移动指定的位数，高位补0，符号位同时移动</span></span><br><span class="line">        <span class="comment">/*</span></span><br><span class="line"><span class="comment">         *</span></span><br><span class="line"><span class="comment">         * 原码：1000 0000 0000 0000 0000 0000 0000 1101</span></span><br><span class="line"><span class="comment">         * 反码：1111 1111 1111 1111 1111 1111 1111 0010</span></span><br><span class="line"><span class="comment">         * 补码：1111 1111 1111 1111 1111 1111 1111 0011</span></span><br><span class="line"><span class="comment">         *</span></span><br><span class="line"><span class="comment">         * 0011 1111 1111 1111 1111 1111 1111 1100 向右移动2位，负数高位补0</span></span><br><span class="line"><span class="comment">         *</span></span><br><span class="line"><span class="comment">         * 原码：0011 1111 1111 1111 1111 1111 1111 1100</span></span><br><span class="line"><span class="comment">         *</span></span><br><span class="line"><span class="comment">         * 值为 1073741820</span></span><br><span class="line"><span class="comment">         *</span></span><br><span class="line"><span class="comment">         * */</span></span><br><span class="line">        <span class="keyword">byte</span> a = -<span class="number">13</span>;</span><br><span class="line">        System.out.println(a &gt;&gt;&gt; <span class="number">2</span>);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"></span><br></pre></td></tr></table></figure></li>
</ul>
<h3 id="java-中的类型转换"><a href="#java-中的类型转换" class="headerlink" title="java 中的类型转换"></a>java 中的类型转换</h3><ul>
<li><p>自动类型转换</p>
<ul>
<li>小类型转大类型，取值范围比原有的大不回出现问题</li>
</ul>
</li>
<li><p>强制类型转换</p>
<ul>
<li>大类型转小类型，注意数据的正确性，否则容易出现错误</li>
</ul>
</li>
<li><p>示例代码：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Demo08</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">        <span class="comment">// java 中数值常量默认为 int 浮点默认为 double</span></span><br><span class="line">        <span class="comment">// 赋值时右侧常量 jvm 会隐式转换类型</span></span><br><span class="line">        <span class="comment">// 如果超过右侧类型范围不会转换</span></span><br><span class="line">        <span class="keyword">byte</span> myByte = <span class="number">116</span>;</span><br><span class="line">        <span class="keyword">short</span> myShort = <span class="number">31000</span>;</span><br><span class="line"></span><br><span class="line">        <span class="comment">// 自动类型转换，小转大，随便转，必须类型匹配</span></span><br><span class="line">        <span class="keyword">int</span> myInt = myByte;</span><br><span class="line"></span><br><span class="line">        <span class="comment">// float 使用的是科学计数法，表示范围比 long 大</span></span><br><span class="line">        <span class="comment">// 但是转换过程中如果出现较大值，也会存在精度丢失</span></span><br><span class="line">        <span class="keyword">long</span> myLong = <span class="number">123123187236817236L</span>;</span><br><span class="line">        <span class="keyword">float</span> myFloat = myLong;</span><br><span class="line">        System.out.println(myFloat);</span><br><span class="line"></span><br><span class="line">        <span class="comment">// char 不支持直接赋予 int 值变量</span></span><br><span class="line"><span class="comment">//        char myChar = myInt;</span></span><br><span class="line">        <span class="comment">// boolean 只能跟 boolean 玩</span></span><br><span class="line"><span class="comment">//        boolean myBoolean = myByte;</span></span><br><span class="line"></span><br><span class="line">        <span class="comment">// 强制类型转换</span></span><br><span class="line">        <span class="keyword">char</span> myChar = (<span class="keyword">char</span>)myByte;</span><br><span class="line"></span><br><span class="line">        <span class="comment">// 类型之间的运算</span></span><br><span class="line">        <span class="comment">// 右侧为常量，并且运算结果没有超出左侧类型范围会隐式强转</span></span><br><span class="line">        <span class="comment">// myByte = (byte)(6 + 7)</span></span><br><span class="line">        myByte = <span class="number">6</span> + <span class="number">7</span>;</span><br><span class="line">        System.out.println(myByte);</span><br><span class="line"></span><br><span class="line">        <span class="comment">// 如果右侧出现了变量，需要手动强转</span></span><br><span class="line"><span class="comment">//        myByte = 6 + myInt;</span></span><br><span class="line"></span><br><span class="line">        <span class="comment">// 小类型与大类型运算都会进行类型提升</span></span><br><span class="line"><span class="comment">//        myByte = 6 + myShort;</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line">        <span class="comment">// char 类型进行运算会使用 ASCII 码 int类型</span></span><br><span class="line">        myChar = <span class="string">&#x27;a&#x27;</span>;</span><br><span class="line">        myByte = (<span class="keyword">byte</span>) (myShort+myChar);</span><br><span class="line"></span><br><span class="line">        <span class="comment">// char 类型也可以直接使用数值赋值</span></span><br><span class="line">        myChar = <span class="number">123</span>;</span><br><span class="line">        System.out.println(myChar);</span><br><span class="line"></span><br><span class="line">        <span class="comment">// 布尔类型强制转换也转不动</span></span><br><span class="line">        <span class="keyword">boolean</span> myBoolean = <span class="keyword">true</span>;</span><br><span class="line"><span class="comment">//        myByte = (Byte)myBoolean;</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"></span><br></pre></td></tr></table></figure></li>
<li><p>类型转换总结：</p>
<ul>
<li>java 中常量值，整型默认为 int，浮点型默认为double</li>
<li>byte/short/char 进行运算时，会自动提升至 int 类型再进行运算</li>
<li>boolean 类型不能发生数据转换</li>
<li>javac 编译器，在编译的过程中会进行隐式的类型转换，右侧常量赋值给左侧变量并且值得大小没有超过左侧范围。如果右侧出现变量，可能会报错</li>
</ul>
</li>
</ul>
<h2 id="java-中的常用引用类型"><a href="#java-中的常用引用类型" class="headerlink" title="java 中的常用引用类型"></a>java 中的常用引用类型</h2><ul>
<li><p>String 用于存储字符串(多个字符，使用双引号包裹)</p>
<ul>
<li>String 字面值得比较使用 str1.equals(str2)</li>
</ul>
</li>
<li><p>Scanner 扫描对象，扫描用户在控制台中输入得内容</p>
<ul>
<li>接收用户输入的值：next()/nextInt()/nextDouble()</li>
<li>判断用户输入的值类型：hasNext()/hasNextInt()/hasNextDouble()</li>
<li>接收用户输入带空格内容：nextLine()</li>
</ul>
</li>
<li><p>在字符串中使用 \ 通常用于转义</p>
<ul>
<li>换行：\n</li>
<li>回车：\r</li>
<li>缩进：\t</li>
<li>在字符中输出双引号：\“</li>
</ul>
</li>
<li><p>Math：数学计算工具类</p>
</li>
<li><p>DecimalFormat：格式化十进制数字</p>
</li>
<li><p>使用以上所有的引用类型的步骤</p>
<ul>
<li>导入需要使用的引用类型<ul>
<li>improt xxx.xxx.xxx.xxx</li>
</ul>
</li>
<li>new 创建引用类型实例<ul>
<li>new XXX();</li>
</ul>
</li>
<li>访问实例的方法或属性<ul>
<li>xxx.XXX();</li>
</ul>
</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Demo09</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">        <span class="comment">// String 字符串</span></span><br><span class="line">        String str1 = <span class="keyword">new</span> String(<span class="string">&quot;123&quot;</span>);</span><br><span class="line">        String str2 = <span class="string">&quot;123&quot;</span>;</span><br><span class="line">        System.out.println(str1.equals(str2));</span><br><span class="line"></span><br><span class="line">        <span class="comment">// Scanner 对象，扫描控制</span></span><br><span class="line">        <span class="comment">// new 创建对象</span></span><br><span class="line">        Scanner sc = <span class="keyword">new</span> Scanner(System.in);</span><br><span class="line">        <span class="comment">// 接收值之前给予提示</span></span><br><span class="line">        System.out.println(<span class="string">&quot;请输入任意内容：&quot;</span>);</span><br><span class="line">        <span class="comment">// 使用 scanner 中接收值的方法，next(),nextInt(),nextDouble()</span></span><br><span class="line">        String str = sc.next();</span><br><span class="line"></span><br><span class="line">        System.out.println(<span class="string">&quot;你说的是\&quot;&quot;</span>+str+<span class="string">&quot;\&quot;吗？&quot;</span>);</span><br><span class="line"></span><br><span class="line">        <span class="comment">// 如果用户输入的值与接收类型不匹配则报错</span></span><br><span class="line">        System.out.println(<span class="string">&quot;请输入一个数字：&quot;</span>);</span><br><span class="line">        <span class="comment">// 可以先进行类型判断</span></span><br><span class="line">        <span class="keyword">if</span>(sc.hasNextInt())&#123;</span><br><span class="line">            <span class="keyword">int</span> i = sc.nextInt();</span><br><span class="line">            System.out.println(<span class="string">&quot;你输入的数是 &quot;</span>+i+<span class="string">&quot; 吗？&quot;</span>);</span><br><span class="line">        &#125;<span class="keyword">else</span> &#123;</span><br><span class="line">            sc.next();</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        System.out.println(<span class="string">&quot;请输入想看的电影：&quot;</span>);</span><br><span class="line">        String movie = sc.next();</span><br><span class="line">        System.out.println(<span class="string">&quot;是&quot;</span>+movie+<span class="string">&quot;这个电影吗？&quot;</span>);</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"></span><br></pre></td></tr></table></figure></li>
<li><p>Math</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Demo10</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">        <span class="comment">// Math 数学计算工具类</span></span><br><span class="line">        <span class="comment">// Math 中提供了一个 random() 方法获取一个 [0,1) 随即小数</span></span><br><span class="line">        <span class="keyword">double</span> rd = Math.random();</span><br><span class="line">        System.out.println(rd);</span><br><span class="line"></span><br><span class="line">        <span class="comment">// 获取 [0,100] 的随机整数</span></span><br><span class="line">        <span class="keyword">int</span> rd2 = (<span class="keyword">int</span>)(Math.random()*<span class="number">101</span>);</span><br><span class="line">        System.out.println(rd2);</span><br><span class="line"></span><br><span class="line">        <span class="comment">// 获取 [1000,9999] 的随机整数</span></span><br><span class="line">        <span class="keyword">int</span> rd3 = (<span class="keyword">int</span>)(Math.random()*<span class="number">9000</span>+<span class="number">1000</span>);</span><br><span class="line">        System.out.println(rd3);</span><br><span class="line"></span><br><span class="line">        <span class="comment">// 计算机所有随机都是伪随机</span></span><br><span class="line">        <span class="comment">// 会根据随机种子进行计算随机</span></span><br><span class="line">        Random r1 = <span class="keyword">new</span> Random(<span class="number">200</span>);</span><br><span class="line">        Random r2 = <span class="keyword">new</span> Random(<span class="number">200</span>);</span><br><span class="line">        System.out.println(r1.nextInt(<span class="number">100</span>));</span><br><span class="line">        System.out.println(r1.nextInt(<span class="number">100</span>));</span><br><span class="line">        System.out.println(r1.nextInt(<span class="number">100</span>));</span><br><span class="line">        System.out.println(<span class="string">&quot;==================&quot;</span>);</span><br><span class="line">        System.out.println(r2.nextInt(<span class="number">100</span>));</span><br><span class="line">        System.out.println(r2.nextInt(<span class="number">100</span>));</span><br><span class="line">        System.out.println(r2.nextInt(<span class="number">100</span>));</span><br><span class="line"></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"></span><br></pre></td></tr></table></figure></li>
<li><p>DecimalFormat</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Demo11</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">        <span class="comment">/*</span></span><br><span class="line"><span class="comment">        * DecimalFormat：格式化十进制数字</span></span><br><span class="line"><span class="comment">        *</span></span><br><span class="line"><span class="comment">        * */</span></span><br><span class="line">        <span class="comment">// 创建一个小数</span></span><br><span class="line">        <span class="keyword">double</span> pi = Math.PI;</span><br><span class="line"></span><br><span class="line">        <span class="comment">// 获取整数，位数不足使用0填充</span></span><br><span class="line">        System.out.println(</span><br><span class="line">                <span class="keyword">new</span> DecimalFormat(<span class="string">&quot;0&quot;</span>).format(pi));</span><br><span class="line"></span><br><span class="line">        <span class="comment">// 获取整数，并且保留两位小数，不足用0填充</span></span><br><span class="line">        System.out.println(</span><br><span class="line">                <span class="keyword">new</span> DecimalFormat(<span class="string">&quot;0.00&quot;</span>).format(pi));</span><br><span class="line"></span><br><span class="line">        <span class="comment">// 获取整数两位，并且保留三位小数，不足用0填充，会四舍五入</span></span><br><span class="line">        System.out.println(</span><br><span class="line">                <span class="keyword">new</span> DecimalFormat(<span class="string">&quot;00.000&quot;</span>).format(pi));</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">        pi = <span class="number">325.12376576123</span>;</span><br><span class="line">        <span class="comment">// 获取所有整数部分</span></span><br><span class="line">        System.out.println(</span><br><span class="line">                <span class="keyword">new</span> DecimalFormat(<span class="string">&quot;#&quot;</span>).format(pi));</span><br><span class="line"></span><br><span class="line">        <span class="comment">// 百分比形式显示数字，保留两位小数</span></span><br><span class="line">        System.out.println(</span><br><span class="line">                <span class="keyword">new</span> DecimalFormat(<span class="string">&quot;#.##%&quot;</span>).format(pi));</span><br><span class="line"></span><br><span class="line">        <span class="comment">// 科学计数法显示数字，保留两位小数</span></span><br><span class="line">        System.out.println(</span><br><span class="line">                <span class="keyword">new</span> DecimalFormat(<span class="string">&quot;#.##E0&quot;</span>).format(pi));</span><br><span class="line"></span><br><span class="line">        pi = <span class="number">1287357634</span>;</span><br><span class="line">        <span class="comment">// 每三位逗号分隔</span></span><br><span class="line">        System.out.println(</span><br><span class="line">                <span class="keyword">new</span> DecimalFormat(<span class="string">&quot;,###&quot;</span>).format(pi));</span><br><span class="line"></span><br><span class="line">        <span class="comment">// 数字嵌入文本中</span></span><br><span class="line">        System.out.println(</span><br><span class="line">                <span class="keyword">new</span> DecimalFormat(<span class="string">&quot;光的速度为每秒,###米&quot;</span>)</span><br><span class="line">                        .format(pi));</span><br><span class="line"></span><br><span class="line">        <span class="comment">// 格式化钱数</span></span><br><span class="line">        System.out.println(</span><br><span class="line">                <span class="keyword">new</span> DecimalFormat(<span class="string">&quot;存款为,###.00￥&quot;</span>)</span><br><span class="line">                        .format(pi));</span><br><span class="line">        <span class="comment">/*</span></span><br><span class="line"><span class="comment">        * 总结：</span></span><br><span class="line"><span class="comment">        *   0或#表示整数部分时接收到所有整数部分，使用0时不足位数会使用0填充</span></span><br><span class="line"><span class="comment">        *   0或#对于小数部分时接收指定位数的小数，使用0时不足位使用0填充</span></span><br><span class="line"><span class="comment">        *   逗号表示分隔符，分隔位数由右侧0或#决定</span></span><br><span class="line"><span class="comment">        * */</span></span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"></span><br></pre></td></tr></table></figure></li>
</ul>
<h2 id="java-中的流程控制语句"><a href="#java-中的流程控制语句" class="headerlink" title="java 中的流程控制语句"></a>java 中的流程控制语句</h2><ul>
<li>在程序中，代码的运行的流程控制决定了程序是如何执行的，主要有三大流程控制语句<ul>
<li>顺序控制</li>
<li>分支控制</li>
<li>循环控制</li>
</ul>
</li>
</ul>
<h3 id="顺序结构"><a href="#顺序结构" class="headerlink" title="顺序结构"></a>顺序结构</h3><ul>
<li><p>顺序结构就是按照程序从上至下，由左及右逐行执行，就像我们写文章</p>
</li>
<li><p>java 中的变量就是 先定义再使用</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 错误写法</span></span><br><span class="line"><span class="keyword">int</span> num1 = num2 + <span class="number">3</span>;</span><br><span class="line"><span class="keyword">int</span> num2 = <span class="number">17</span>;</span><br><span class="line"></span><br></pre></td></tr></table></figure></li>
<li><p>目前的代码基本都是顺序结构</p>
</li>
</ul>
<h3 id="分支结构"><a href="#分支结构" class="headerlink" title="分支结构"></a>分支结构</h3><ul>
<li>分支控制语句就是根据不同的条件执行不同的代码分支，最终执行不同的代码</li>
<li>条件是什么，选择了不同的条件就会有不同的结果</li>
</ul>
<h4 id="if分支语句"><a href="#if分支语句" class="headerlink" title="if分支语句"></a>if分支语句</h4><ul>
<li><p>分支控制语句能够让程序有选择性的执行相应的代码</p>
</li>
<li><p>if的结构一般分为三种</p>
<ul>
<li><p>单分支</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">if</span>(判断条件)&#123;</span><br><span class="line">    <span class="comment">// 判断条件为 true 时执行的逻辑代码</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure></li>
<li><p>双分支</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">if</span>(判断条件)&#123;</span><br><span class="line">    <span class="comment">// 判断条件为 true 时执行的逻辑代码</span></span><br><span class="line">&#125;<span class="keyword">else</span> &#123;</span><br><span class="line">    <span class="comment">// 判断条件为 false 时执行的逻辑代码</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure></li>
<li><p>多分支</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">if</span>(判断条件<span class="number">1</span>)&#123;</span><br><span class="line">    <span class="comment">// 判断条件1为 true 时执行的逻辑代码</span></span><br><span class="line">&#125;<span class="keyword">else</span> <span class="keyword">if</span>(判断条件<span class="number">2</span>)&#123;</span><br><span class="line">    <span class="comment">// 判断条件2为 true 时执行的逻辑代码</span></span><br><span class="line">&#125;</span><br><span class="line">...</span><br><span class="line"><span class="keyword">else</span> &#123;</span><br><span class="line">    <span class="comment">// 所有判断条件都为 false 时执行的逻辑代码</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"></span><br></pre></td></tr></table></figure></li>
</ul>
</li>
</ul>
<ul>
<li><p>if 还可以进行嵌套编写</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">if</span>(判断条件)&#123;</span><br><span class="line">    <span class="comment">// 判断条件为 true 时执行的逻辑代码</span></span><br><span class="line">    <span class="keyword">if</span>(判断条件)&#123;</span><br><span class="line">    	<span class="comment">// 判断条件为 true 时执行的逻辑代码</span></span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">	在一个分支结构中又完整的嵌套另一个完整的分支结构</span></span><br><span class="line"><span class="comment">	里面的分支结构成为内层分支，外面的分支称为外层分支</span></span><br><span class="line"><span class="comment">	嵌套分支会影响代码的可读性，建议最好不要超过 3 层</span></span><br><span class="line"><span class="comment">	如果你嵌套了 10 层，神仙来了都看不懂</span></span><br><span class="line"><span class="comment">*/</span></span><br><span class="line"></span><br><span class="line"></span><br></pre></td></tr></table></figure></li>
<li><p>案例展示</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.seecen.day02;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 导包</span></span><br><span class="line"><span class="keyword">import</span> java.util.Scanner;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">TestIf01</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">        <span class="comment">/*</span></span><br><span class="line"><span class="comment">        *</span></span><br><span class="line"><span class="comment">        * 酒驾定义：</span></span><br><span class="line"><span class="comment">        *   驾驶员每100毫升血液含有酒精含量大于等于20毫克</span></span><br><span class="line"><span class="comment">        *   并且100毫升血液含有酒精含量小于80毫克定性为酒后驾车</span></span><br><span class="line"><span class="comment">        *   每100毫升血液含有酒精含量大于等于80毫克定性为醉酒驾车</span></span><br><span class="line"><span class="comment">        *</span></span><br><span class="line"><span class="comment">        * */</span></span><br><span class="line">        <span class="comment">// 用户输入酒精含量：根据不同的酒精含量进行判断</span></span><br><span class="line">        <span class="comment">// 0，创建 Scanner 对象</span></span><br><span class="line">        Scanner sc = <span class="keyword">new</span> Scanner(System.in);</span><br><span class="line">        <span class="comment">// 1，提示用户输入酒精含量</span></span><br><span class="line">        System.out.println(<span class="string">&quot;请输入你酒精含量：&quot;</span>);</span><br><span class="line">        <span class="keyword">if</span>(sc.hasNextInt())&#123;</span><br><span class="line">            <span class="keyword">int</span> alcohol = sc.nextInt();</span><br><span class="line">            <span class="comment">// 如果用户</span></span><br><span class="line">            <span class="keyword">if</span>(alcohol &gt;= <span class="number">80</span>)&#123;</span><br><span class="line">                System.out.println(<span class="string">&quot;你是醉酒驾车！直接带走！&quot;</span>);</span><br><span class="line">            &#125;<span class="keyword">else</span> <span class="keyword">if</span>(alcohol &gt;= <span class="number">20</span>)&#123;</span><br><span class="line">                System.out.println(<span class="string">&quot;你是酒后驾车！亲人两行泪！&quot;</span>);</span><br><span class="line">            &#125;<span class="keyword">else</span> &#123;</span><br><span class="line">                System.out.println(<span class="string">&quot;一路顺风&quot;</span>);</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;<span class="keyword">else</span> &#123;</span><br><span class="line">            sc.next();</span><br><span class="line">            System.out.println(<span class="string">&quot;直接带走！&quot;</span>);</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"></span><br></pre></td></tr></table></figure></li>
</ul>
<h4 id="switch分支语句"><a href="#switch分支语句" class="headerlink" title="switch分支语句"></a>switch分支语句</h4><ul>
<li><p>switch 和 if 的功能十分相似，特别是多分支</p>
</li>
<li><p>但是 switch 只做精准值的判断</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">switch</span>(表达式)&#123;</span><br><span class="line">    <span class="keyword">case</span> 值<span class="number">1</span>:</span><br><span class="line">        <span class="comment">// 满足值1所执行的逻辑代码</span></span><br><span class="line">    <span class="keyword">case</span> 值<span class="number">2</span>:</span><br><span class="line">        <span class="comment">// 满足值2所执行的逻辑代码</span></span><br><span class="line">    ...</span><br><span class="line">    <span class="keyword">default</span>:</span><br><span class="line">        <span class="comment">// 所有值都不匹配所执行的逻辑代码</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// PS：表达式可以为 byte、short、int、char、String</span></span><br><span class="line"><span class="comment">// siwtch 和 if 非常相似，只要 switch 能做的 if 都做</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// 注意使用 break 关键字退出 switch ，警惕 switch 的穿透性</span></span><br><span class="line"></span><br><span class="line"></span><br></pre></td></tr></table></figure></li>
<li><p>案例展示</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.seecen.day02;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> java.util.Scanner;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">TestSwitch01</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">        <span class="comment">/*</span></span><br><span class="line"><span class="comment">        * 用户输入星期号码(1-7)</span></span><br><span class="line"><span class="comment">        *   1-5都是上班</span></span><br><span class="line"><span class="comment">        *   6需要提升自己，偷偷学习</span></span><br><span class="line"><span class="comment">        *   7陪伴家人</span></span><br><span class="line"><span class="comment">        *</span></span><br><span class="line"><span class="comment">        * */</span></span><br><span class="line">        <span class="comment">// 0，创建 Scanner 对象</span></span><br><span class="line">        Scanner sc = <span class="keyword">new</span> Scanner(System.in);</span><br><span class="line">        <span class="comment">// 1，提示用户输入星期号码</span></span><br><span class="line">        System.out.println(<span class="string">&quot;请输入今天星期几(1-7)&quot;</span>);</span><br><span class="line">        <span class="keyword">if</span>(sc.hasNextInt())&#123;</span><br><span class="line">            <span class="keyword">int</span> week = sc.nextInt();</span><br><span class="line">            <span class="comment">// 使用 switch 进行星期判断</span></span><br><span class="line">            <span class="keyword">switch</span> (week)&#123;</span><br><span class="line">                <span class="keyword">case</span> <span class="number">1</span>:</span><br><span class="line">                <span class="keyword">case</span> <span class="number">5</span>:</span><br><span class="line">                <span class="keyword">case</span> <span class="number">4</span>:</span><br><span class="line">                <span class="keyword">case</span> <span class="number">3</span>:</span><br><span class="line">                <span class="keyword">case</span> <span class="number">2</span>:</span><br><span class="line">                    System.out.println(<span class="string">&quot;今天需要上班&quot;</span>);</span><br><span class="line">                    <span class="comment">// 使用 beak 退出 switch</span></span><br><span class="line">                    <span class="keyword">break</span>;</span><br><span class="line">                <span class="keyword">case</span> <span class="number">6</span>:</span><br><span class="line">                    System.out.println(<span class="string">&quot;今天需要偷偷提升&quot;</span>);</span><br><span class="line">                    <span class="keyword">break</span>;</span><br><span class="line">                <span class="keyword">case</span> <span class="number">7</span>:</span><br><span class="line">                    System.out.println(<span class="string">&quot;今天需要陪伴家人&quot;</span>);</span><br><span class="line">                    <span class="keyword">break</span>;</span><br><span class="line">                <span class="keyword">default</span>:</span><br><span class="line">                    System.out.println(<span class="string">&quot;哦，原来是火星人啊&quot;</span>);</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;<span class="keyword">else</span>&#123;</span><br><span class="line">            sc.next();</span><br><span class="line">            System.out.println(<span class="string">&quot;星期八吗？&quot;</span>);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"></span><br></pre></td></tr></table></figure></li>
</ul>
<h4 id="选择分支总结"><a href="#选择分支总结" class="headerlink" title="选择分支总结"></a>选择分支总结</h4><ul>
<li>如果判断的具体值不多，而且符合 byte、short、int、char、String、enum，虽然两种分支语句都可以使用，但是这里推荐使用 switch</li>
<li>其他情况：对区间值进行判断，结果为返回 boolean，使用 if 结构，if 适用范围更广泛</li>
<li>总的来讲：精准值的判断使用 switch，区间值判断使用 if</li>
</ul>
<h3 id="循环结构"><a href="#循环结构" class="headerlink" title="循环结构"></a>循环结构</h3><ul>
<li>循环就是重复的执行同一个逻辑行为，例如：说一百遍 你好，上100层楼，数1-100数字，等等</li>
<li>如果以上内容一行一行的手敲代码，人会累死，并且会被开除。所以需要通过循环轻松解决</li>
</ul>
<h4 id="for-循环"><a href="#for-循环" class="headerlink" title="for 循环"></a>for 循环</h4><ul>
<li><p>for循环的基本语法：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// for循环的基础语法格式</span></span><br><span class="line"><span class="keyword">for</span>(变量声明;判断条件;循环后的操作)&#123;</span><br><span class="line">    <span class="comment">// 满足判断条件后重复执行的代码</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// for 循环的常用结构</span></span><br><span class="line"><span class="keyword">for</span>(<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; <span class="number">100</span>; i++)&#123;</span><br><span class="line">    <span class="comment">// 这里的代码会重复执行 100 次</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"></span><br></pre></td></tr></table></figure></li>
<li><p>for循环的代码执行顺序</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// for循环的基础语法格式</span></span><br><span class="line"><span class="keyword">for</span>(操作<span class="number">1</span>;判断条件b;操作<span class="number">2</span>)&#123;</span><br><span class="line">    <span class="comment">// 满足判断条件后重复执行的代码</span></span><br><span class="line">    操作<span class="number">3</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">第一次循环：<span class="number">1</span> ==&gt; b ==&gt; <span class="number">3</span> ==&gt; <span class="number">2</span></span><br><span class="line">后续的循环：b ==&gt; <span class="number">3</span> ==&gt; <span class="number">2</span> ==&gt; b ...</span><br><span class="line">最后一次循环：<span class="number">3</span> ==&gt; <span class="number">2</span> ==&gt; b</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br></pre></td></tr></table></figure></li>
<li><p>循环中的关键字</p>
<ul>
<li>break 中断当前循环，后续代码不执行</li>
<li>continue 中断本次循环，后续代码本次不执行</li>
</ul>
</li>
</ul>
<h4 id="while-循环"><a href="#while-循环" class="headerlink" title="while 循环"></a>while 循环</h4><ul>
<li><p>while 循环基本语法</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">while</span>(判断条件)&#123;</span><br><span class="line">    <span class="comment">// 条件为 true 时执行的代码</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure></li>
<li><p>do…while 循环基本语法</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">do</span>&#123;</span><br><span class="line">    <span class="comment">// 无论条件是否为 true 都会执行一次</span></span><br><span class="line">    <span class="comment">// 后续根据判断条件决定是否执行</span></span><br><span class="line">&#125;<span class="keyword">while</span>(判断条件)</span><br><span class="line"></span><br><span class="line"></span><br></pre></td></tr></table></figure></li>
</ul>
<h4 id="循环中的特殊情况"><a href="#循环中的特殊情况" class="headerlink" title="循环中的特殊情况"></a>循环中的特殊情况</h4><ul>
<li><p>for循环的定义的变量作用域问题</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.seecen.day02;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">TestFor01</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">        <span class="comment">// 通过 for 循环输出一百次 我很帅</span></span><br><span class="line">        <span class="keyword">for</span>(<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; <span class="number">100</span>; i++)&#123;</span><br><span class="line">            System.out.println(<span class="string">&quot;我很帅&quot;</span>);</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="comment">// for 循环中变量的作用域</span></span><br><span class="line">        <span class="keyword">for</span>(<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; <span class="number">100</span>; i++)&#123;</span><br><span class="line">            System.out.println(<span class="string">&quot;我很帅&quot;</span>);</span><br><span class="line"></span><br><span class="line">            <span class="comment">// 循环嵌套使用时不能使用同名变量</span></span><br><span class="line">            <span class="keyword">for</span>(<span class="keyword">int</span> j = <span class="number">0</span>; j &lt; <span class="number">100</span>; j++)&#123;</span><br><span class="line">                System.out.println(<span class="string">&quot;我很帅&quot;</span>);</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="comment">// 错误写法</span></span><br><span class="line">            <span class="comment">// j = 18;</span></span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"></span><br></pre></td></tr></table></figure></li>
<li><p>循环中的标记</p>
<ul>
<li><p>break</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.seecen.day02;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">FlagBreak</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">        <span class="comment">// fori</span></span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; <span class="number">3</span>; i++) &#123;</span><br><span class="line">            System.out.println(<span class="string">&quot;外层循环：&quot;</span>+i);</span><br><span class="line">            <span class="keyword">for</span> (<span class="keyword">int</span> j = <span class="number">0</span>; j &lt; <span class="number">3</span>; j++) &#123;</span><br><span class="line">                System.out.println(<span class="string">&quot;内层循环：&quot;</span>+j);</span><br><span class="line">                <span class="keyword">if</span>(j == <span class="number">1</span>)&#123;</span><br><span class="line">                    <span class="keyword">break</span>;</span><br><span class="line">                &#125;</span><br><span class="line">            &#125;</span><br><span class="line">            System.out.println(<span class="string">&quot;===================&quot;</span>);</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        System.out.println(<span class="string">&quot;**************************&quot;</span>);</span><br><span class="line"></span><br><span class="line">        <span class="comment">// 添加标记</span></span><br><span class="line">        flag:<span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; <span class="number">3</span>; i++) &#123;</span><br><span class="line">            System.out.println(<span class="string">&quot;外层循环：&quot;</span>+i);</span><br><span class="line">            <span class="keyword">for</span> (<span class="keyword">int</span> j = <span class="number">0</span>; j &lt; <span class="number">3</span>; j++) &#123;</span><br><span class="line">                System.out.println(<span class="string">&quot;内层循环：&quot;</span>+j);</span><br><span class="line">                <span class="keyword">if</span>(j == <span class="number">1</span>)&#123;</span><br><span class="line">                    <span class="keyword">break</span> flag;</span><br><span class="line">                &#125;</span><br><span class="line">            &#125;</span><br><span class="line">            System.out.println(<span class="string">&quot;===================&quot;</span>);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"></span><br></pre></td></tr></table></figure></li>
<li><p>continue</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.seecen.day02;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">FlagContinue</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">        <span class="comment">// fori</span></span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; <span class="number">3</span>; i++) &#123;</span><br><span class="line">            System.out.println(<span class="string">&quot;外层循环：&quot;</span>+i);</span><br><span class="line">            <span class="keyword">for</span> (<span class="keyword">int</span> j = <span class="number">0</span>; j &lt; <span class="number">3</span>; j++) &#123;</span><br><span class="line"></span><br><span class="line">                <span class="keyword">if</span>(j == <span class="number">1</span>)&#123;</span><br><span class="line">                    <span class="comment">// 跳过本次循环</span></span><br><span class="line">                    <span class="keyword">continue</span>;</span><br><span class="line">                &#125;</span><br><span class="line">                System.out.println(<span class="string">&quot;内层循环：&quot;</span>+j);</span><br><span class="line">            &#125;</span><br><span class="line">            System.out.println(<span class="string">&quot;===================&quot;</span>);</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        System.out.println(<span class="string">&quot;**************************&quot;</span>);</span><br><span class="line"></span><br><span class="line">        <span class="comment">// 添加标记</span></span><br><span class="line">        flag:<span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; <span class="number">3</span>; i++) &#123;</span><br><span class="line">            System.out.println(<span class="string">&quot;外层循环：&quot;</span>+i);</span><br><span class="line">            <span class="keyword">for</span> (<span class="keyword">int</span> j = <span class="number">0</span>; j &lt; <span class="number">3</span>; j++) &#123;</span><br><span class="line">                <span class="keyword">if</span>(j == <span class="number">1</span>)&#123;</span><br><span class="line">                    <span class="keyword">continue</span> flag;</span><br><span class="line">                &#125;</span><br><span class="line">                System.out.println(<span class="string">&quot;内层循环：&quot;</span>+j);</span><br><span class="line">            &#125;</span><br><span class="line">            System.out.println(<span class="string">&quot;===================&quot;</span>);</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"></span><br></pre></td></tr></table></figure></li>
</ul>
</li>
</ul>
<h2 id="java-中的数组"><a href="#java-中的数组" class="headerlink" title="java 中的数组"></a>java 中的数组</h2><h3 id="数组的基本概念"><a href="#数组的基本概念" class="headerlink" title="数组的基本概念"></a>数组的基本概念</h3><ul>
<li><p>java 中的数字 Array</p>
<ul>
<li>存储一系列值的容器，必须是相同类型，长度固定</li>
</ul>
</li>
<li><p>数组的特点</p>
<ol>
<li>数组是引用类型</li>
<li>数组中的多个数据必须类型一致</li>
<li>数组的长度在程序运行期间不可改变的</li>
</ol>
</li>
<li><p>数组的初始化(声明数组并且给其赋值)</p>
<ul>
<li>在内存中创建一个数组，并且向其中赋予一些值</li>
</ul>
</li>
<li><p>常见的初始化方式</p>
<ul>
<li><p>动态初始化（指定其长度，没有赋值内容）</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 动态初始化数组</span></span><br><span class="line">数据类型[] 数组的名称 = <span class="keyword">new</span> 数据类型[数组长度];</span><br><span class="line"></span><br><span class="line"></span><br></pre></td></tr></table></figure></li>
<li><p>静态初始化（指定其内容）</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 静态初始化数组</span></span><br><span class="line">数据类型[] 数组的名称 = <span class="keyword">new</span> 数据类型[]&#123;元素<span class="number">1</span>,元素<span class="number">2</span>,元素<span class="number">3</span>,...&#125;</span><br><span class="line">数据类型[] 数组的名称 = &#123;元素<span class="number">1</span>,元素<span class="number">2</span>,元素<span class="number">3</span>,...&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure></li>
</ul>
</li>
<li><p>代码展示</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">MyArray</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">        <span class="comment">// java 中的数组</span></span><br><span class="line">        <span class="comment">// 动态初始化</span></span><br><span class="line">        <span class="comment">// 数据类型[] 数组的名称 = new 数据类型[数组长度];</span></span><br><span class="line">        <span class="comment">// 动态创建一个 int 类型长度为 7 的数组</span></span><br><span class="line">        <span class="keyword">int</span>[] arr1 = <span class="keyword">new</span> <span class="keyword">int</span>[<span class="number">7</span>];</span><br><span class="line">        <span class="comment">// 静态初始化</span></span><br><span class="line">        <span class="comment">// 数据类型[] 数组的名称 = new 数据类型[]&#123;元素1,元素2,元素3,...&#125;</span></span><br><span class="line">        <span class="keyword">int</span>[] arr2 = <span class="keyword">new</span> <span class="keyword">int</span>[]&#123;<span class="number">35</span>, <span class="number">16</span>, <span class="number">23</span>, <span class="number">38</span>, <span class="number">97</span>, <span class="number">63</span>&#125;;</span><br><span class="line">        <span class="comment">// 数据类型[] 数组的名称 = &#123;元素1,元素2,元素3,...&#125;</span></span><br><span class="line">        <span class="keyword">int</span>[] arr3 = &#123;<span class="number">35</span>, <span class="number">16</span>, <span class="number">23</span>, <span class="number">38</span>, <span class="number">97</span>, <span class="number">63</span>&#125;;</span><br><span class="line"></span><br><span class="line">        <span class="comment">// 打印数组 [I@1540e19d</span></span><br><span class="line">        <span class="comment">// print 方法底层打印的是：全限定类名 + @ + 哈希码</span></span><br><span class="line">        <span class="comment">// [I ：[ 表示为数组 ，I 表示为 int 类型</span></span><br><span class="line">        System.out.println(arr3);</span><br><span class="line"></span><br><span class="line">        <span class="comment">// 访问数组中的元素</span></span><br><span class="line">        <span class="comment">// 根据下标访问元素，访问 arr3 的第4个元素</span></span><br><span class="line">        System.out.println(arr3[<span class="number">3</span>]);</span><br><span class="line">        System.out.println(arr3[<span class="number">4</span>]);</span><br><span class="line">        System.out.println(arr3[<span class="number">5</span>]);</span><br><span class="line">        <span class="comment">// 访问超过数组长度的下标就会有问题</span></span><br><span class="line"><span class="comment">//        System.out.println(arr3[6]);</span></span><br><span class="line"></span><br><span class="line">        System.out.println(<span class="string">&quot;===========================&quot;</span>);</span><br><span class="line"></span><br><span class="line">        <span class="comment">// 打印数组中的每一个元素</span></span><br><span class="line">        <span class="comment">// 可以通过数组的 length 属性进行循环遍历</span></span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; arr3.length; i++) &#123;</span><br><span class="line">            <span class="comment">// 循环中取出的元素就是 arr3[i]</span></span><br><span class="line">            System.out.println(arr3[i]);</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure></li>
<li><p>提问：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1</span>，数组不初始化能赋值吗？</span><br><span class="line">不能，引用类型必须初始化后才能赋值</span><br><span class="line">    </span><br><span class="line">    </span><br><span class="line"><span class="number">2</span>，初始化之后，如果没有赋值，每个元素会不会有默认值？</span><br><span class="line">只要数组进行了初始化操作，元素就会有值</span><br><span class="line">    </span><br><span class="line">    </span><br><span class="line"><span class="number">3</span>，数组能不能越界赋值或访问？</span><br><span class="line">不能，会出现错误</span><br><span class="line"></span><br></pre></td></tr></table></figure></li>
<li><p>数组的概念总结</p>
<ol>
<li>数组一旦创建，在运行期间长度不可改变</li>
<li>数组中每一个位置只能存一个值，多了会进行覆盖</li>
<li>数组是引用类型，创建之后给没有赋值的元素进行默认赋值<ul>
<li>整数类型：0</li>
<li>浮点类型：0.0</li>
<li>字符类型：’\u0000’ (unicode 码表中的不可见字符)</li>
<li>布尔类型：false</li>
<li>引用类型：null</li>
</ul>
</li>
<li>元素下标从 0 开始，下标必须在指定的范围内使用，如果超出范围会报错：数组下标越界异常</li>
<li>数组自带 length 属性，最后一位元素下标是 length-1 </li>
<li>数组中可以存储基本类型，也可是引用类型，但是必须都是相同类型</li>
</ol>
</li>
</ul>
<ul>
<li>数组的进阶：<ul>
<li>数组是一种最基础的数据结构，是 表 的一种，是一种 二维线性表<ul>
<li>线性表（linear list）是数据结构的一种，一个线性表是n个具有相同特性的数据元素的有限序列</li>
<li>线性表中数据元素之间的关系是一对一的关系，即除了第一个和最后一个数据元素之外，其它数据元素都是首尾相接的</li>
</ul>
</li>
<li>简单来讲：线性表就是具有相同数据类型的 n(n&gt;=0) 个 数据元素 的有限序列<ul>
<li>有穷性：表中的元素个数有限</li>
<li>有序性：表中的每个元素都具有逻辑上的顺序</li>
<li>同一性：表中元素的数据类型相同的，表示占用的内存空间也是相同的</li>
<li>表中的每个元素都是单个元素，都是数据元素</li>
<li>表中的元素都具有抽象性，只要考虑元素间的多级关系，不需要考虑其中的数据内容</li>
</ul>
</li>
</ul>
</li>
</ul>
<h3 id="数组练习"><a href="#数组练习" class="headerlink" title="数组练习"></a>数组练习</h3><ul>
<li><p>找最大值</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">FindMax</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">        <span class="comment">/*</span></span><br><span class="line"><span class="comment">        *</span></span><br><span class="line"><span class="comment">        *   静态创建一个 int 数组，长度为 8</span></span><br><span class="line"><span class="comment">        *   找出其中的最大值下标</span></span><br><span class="line"><span class="comment">        *   打印输出其最大值下标和具体的最大值</span></span><br><span class="line"><span class="comment">        * */</span></span><br><span class="line">        <span class="keyword">int</span>[] arr = &#123;<span class="number">63</span>,<span class="number">26</span>,<span class="number">18</span>,<span class="number">35</span>,<span class="number">96</span>,<span class="number">16</span>,<span class="number">25</span>,<span class="number">82</span>&#125;;</span><br><span class="line">        <span class="comment">// 创建一个遍历存储最大值的下标</span></span><br><span class="line">        <span class="keyword">int</span> max = <span class="number">0</span>;</span><br><span class="line"></span><br><span class="line">        <span class="comment">// 通过循环遍历数组</span></span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; arr.length; i++) &#123;</span><br><span class="line">            <span class="comment">// 数组中的每个元素 arr[i]</span></span><br><span class="line">            <span class="keyword">if</span>(arr[i] &gt; arr[max])&#123;</span><br><span class="line">                <span class="comment">// 当前元素比最大值要大，记录当前元素下标</span></span><br><span class="line">                max = i;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        System.out.println(<span class="string">&quot;数组中的最大值为：&quot;</span>+arr[max]+<span class="string">&quot;下标为：&quot;</span>+max);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"></span><br></pre></td></tr></table></figure></li>
<li><p>找最小值</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">FindMin</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">        <span class="comment">/*</span></span><br><span class="line"><span class="comment">         *   静态创建一个 int 数组，长度为 8</span></span><br><span class="line"><span class="comment">         *   找出其中的最小值下标</span></span><br><span class="line"><span class="comment">         *   打印输出其最小值下标和具体的最小值</span></span><br><span class="line"><span class="comment">         * */</span></span><br><span class="line">        <span class="keyword">int</span>[] arr = &#123;<span class="number">63</span>,<span class="number">26</span>,<span class="number">18</span>,<span class="number">35</span>,<span class="number">96</span>,<span class="number">16</span>,<span class="number">25</span>,<span class="number">82</span>&#125;;</span><br><span class="line">        <span class="comment">// 创建一个遍历存储最小值的下标</span></span><br><span class="line">        <span class="keyword">int</span> min = <span class="number">0</span>;</span><br><span class="line"></span><br><span class="line">        <span class="comment">// 通过循环遍历数组</span></span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; arr.length; i++) &#123;</span><br><span class="line">            <span class="comment">// 数组中的每个元素 arr[i]</span></span><br><span class="line">            <span class="keyword">if</span>(arr[i] &lt; arr[min])&#123;</span><br><span class="line">                <span class="comment">// 当前元素比最小值要小，记录当前元素下标</span></span><br><span class="line">                min = i;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        System.out.println(<span class="string">&quot;数组中的最大值为：&quot;</span>+arr[min]+<span class="string">&quot;下标为：&quot;</span>+min);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure></li>
<li><p>打印数组</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">PrintArray</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">        <span class="comment">/*</span></span><br><span class="line"><span class="comment">        *</span></span><br><span class="line"><span class="comment">        * 静态创建一个 int 数组，长度为 8</span></span><br><span class="line"><span class="comment">        * 将数组所有的元素用以下格式打印出来</span></span><br><span class="line"><span class="comment">        *   [63, 26, 18, 35, 96, 16, 25, 82]</span></span><br><span class="line"><span class="comment">        * */</span></span><br><span class="line">        <span class="keyword">int</span>[] arr = &#123;<span class="number">63</span>,<span class="number">26</span>,<span class="number">18</span>,<span class="number">35</span>,<span class="number">96</span>,<span class="number">16</span>,<span class="number">25</span>,<span class="number">82</span>&#125;;</span><br><span class="line"></span><br><span class="line">        System.out.print(<span class="string">&quot;[&quot;</span>);</span><br><span class="line">        <span class="comment">// 通过循环打印数组</span></span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; arr.length; i++) &#123;</span><br><span class="line">            <span class="keyword">if</span> (i == arr.length - <span class="number">1</span>)&#123;</span><br><span class="line">                System.out.print(arr[i]);</span><br><span class="line">                <span class="keyword">break</span>;</span><br><span class="line">            &#125;</span><br><span class="line">            System.out.print(arr[i]+<span class="string">&quot;, &quot;</span>);</span><br><span class="line">        &#125;</span><br><span class="line">        System.out.println(<span class="string">&quot;]&quot;</span>);</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure></li>
<li><p>元素位置调换</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">PrintArray</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">        <span class="comment">/*</span></span><br><span class="line"><span class="comment">        *</span></span><br><span class="line"><span class="comment">        * 静态创建一个 int 数组，长度为 8</span></span><br><span class="line"><span class="comment">        * 将数组所有的元素用以下格式打印出来</span></span><br><span class="line"><span class="comment">        *   [63, 26, 18, 35, 96, 16, 25, 82]</span></span><br><span class="line"><span class="comment">        * */</span></span><br><span class="line">        <span class="keyword">int</span>[] arr = &#123;<span class="number">63</span>,<span class="number">26</span>,<span class="number">18</span>,<span class="number">35</span>,<span class="number">96</span>,<span class="number">16</span>,<span class="number">25</span>,<span class="number">82</span>&#125;;</span><br><span class="line"></span><br><span class="line">        System.out.print(<span class="string">&quot;[&quot;</span>);</span><br><span class="line">        <span class="comment">// 通过循环打印数组</span></span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; arr.length; i++) &#123;</span><br><span class="line">            <span class="keyword">if</span> (i == arr.length - <span class="number">1</span>)&#123;</span><br><span class="line">                System.out.print(arr[i]);</span><br><span class="line">                <span class="keyword">break</span>;</span><br><span class="line">            &#125;</span><br><span class="line">            System.out.print(arr[i]+<span class="string">&quot;, &quot;</span>);</span><br><span class="line">        &#125;</span><br><span class="line">        System.out.println(<span class="string">&quot;]&quot;</span>);</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure></li>
<li><p>查找元素位置</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">FindFirst</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">        <span class="comment">/*</span></span><br><span class="line"><span class="comment">        * 通过随机数生(1-100)成一个长度为 10 的 int 数组</span></span><br><span class="line"><span class="comment">        *   让用户输入一个数字，查找该数组中是否出现</span></span><br><span class="line"><span class="comment">        *   如果出现告知用户该数字第一次出现的位置(index+1)</span></span><br><span class="line"><span class="comment">        *   如果没有出现则告知用户该数字没有找到</span></span><br><span class="line"><span class="comment">        * */</span></span><br><span class="line"></span><br><span class="line">        <span class="keyword">int</span>[] arr = <span class="keyword">new</span> <span class="keyword">int</span>[<span class="number">10</span>];</span><br><span class="line">        <span class="comment">// 通过循环生成长度为 10 的数组</span></span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; arr.length; i++) &#123;</span><br><span class="line">            <span class="comment">// 通过随机数进行赋值</span></span><br><span class="line">            arr[i] = (<span class="keyword">int</span>)(Math.random()*<span class="number">100</span>+<span class="number">1</span>);</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="comment">// 打印数组</span></span><br><span class="line">        System.out.print(<span class="string">&quot;[&quot;</span>);</span><br><span class="line">        <span class="comment">// 通过循环打印数组</span></span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; arr.length; i++) &#123;</span><br><span class="line">            <span class="keyword">if</span> (i == arr.length - <span class="number">1</span>)&#123;</span><br><span class="line">                System.out.print(arr[i]);</span><br><span class="line">                <span class="keyword">break</span>;</span><br><span class="line">            &#125;</span><br><span class="line">            System.out.print(arr[i]+<span class="string">&quot;, &quot;</span>);</span><br><span class="line">        &#125;</span><br><span class="line">        System.out.println(<span class="string">&quot;]&quot;</span>);</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">        <span class="comment">// 创建 Scanner 对象</span></span><br><span class="line">        Scanner sc = <span class="keyword">new</span> Scanner(System.in);</span><br><span class="line">        System.out.println(<span class="string">&quot;请输入一个(1-100)的整数进行查找：&quot;</span>);</span><br><span class="line">        <span class="keyword">if</span>(sc.hasNextInt())&#123;</span><br><span class="line">            <span class="comment">// 接收用户输入的整数</span></span><br><span class="line">            <span class="keyword">int</span> num = sc.nextInt();</span><br><span class="line">            <span class="comment">// 记录查找到的元素下标</span></span><br><span class="line">            <span class="keyword">int</span> index = -<span class="number">1</span>;</span><br><span class="line"></span><br><span class="line">            <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; arr.length; i++) &#123;</span><br><span class="line">                <span class="comment">// 判断用户输入值是否和数组中的元素相等</span></span><br><span class="line">                <span class="keyword">if</span>(arr[i] == num)&#123;</span><br><span class="line">                    <span class="comment">// 记录下标</span></span><br><span class="line">                    index = i;</span><br><span class="line">                    <span class="comment">// 已经找到第一次，结束查找</span></span><br><span class="line">                    <span class="keyword">break</span>;</span><br><span class="line">                &#125;</span><br><span class="line">            &#125;</span><br><span class="line"></span><br><span class="line">            <span class="keyword">if</span>(index != -<span class="number">1</span>)&#123;</span><br><span class="line">                System.out.println(<span class="string">&quot;该数字出现在第&quot;</span>+(index+<span class="number">1</span>)+<span class="string">&quot;位&quot;</span>);</span><br><span class="line">            &#125;<span class="keyword">else</span> &#123;</span><br><span class="line">                System.out.println(<span class="string">&quot;该数字没有找到&quot;</span>);</span><br><span class="line">            &#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">        &#125;<span class="keyword">else</span> &#123;</span><br><span class="line">            sc.next();</span><br><span class="line">            System.out.println(<span class="string">&quot;请输入一个整数！&quot;</span>);</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure></li>
<li><p>数组元素反转</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">ReversArray</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">        <span class="comment">/*</span></span><br><span class="line"><span class="comment">        *</span></span><br><span class="line"><span class="comment">        * 元素反转：</span></span><br><span class="line"><span class="comment">        *   通过随机数生(1-100)成一个长度为 9 的 int 数组</span></span><br><span class="line"><span class="comment">        *   将该数组中的元素进行位置反转</span></span><br><span class="line"><span class="comment">        *   &#123;1,2,3,4,5,6,7,8,9&#125; ==&gt; &#123;9,8,7,6,5,4,3,2,1&#125;</span></span><br><span class="line"><span class="comment">        *   输出反转前和反转后</span></span><br><span class="line"><span class="comment">        *   不创建新的数组；</span></span><br><span class="line"><span class="comment">        * */</span></span><br><span class="line">        <span class="keyword">int</span>[] arr = <span class="keyword">new</span> <span class="keyword">int</span>[<span class="number">9</span>];</span><br><span class="line">        <span class="comment">// 通过循环生成长度为 10 的数组</span></span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; arr.length; i++) &#123;</span><br><span class="line">            <span class="comment">// 通过随机数进行赋值</span></span><br><span class="line">            arr[i] = (<span class="keyword">int</span>)(Math.random()*<span class="number">100</span>+<span class="number">1</span>);</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        System.out.print(<span class="string">&quot;[&quot;</span>);</span><br><span class="line">        <span class="comment">// 通过循环打印数组</span></span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; arr.length; i++) &#123;</span><br><span class="line">            <span class="keyword">if</span> (i == arr.length - <span class="number">1</span>)&#123;</span><br><span class="line">                System.out.print(arr[i]);</span><br><span class="line">                <span class="keyword">break</span>;</span><br><span class="line">            &#125;</span><br><span class="line">            System.out.print(arr[i]+<span class="string">&quot;, &quot;</span>);</span><br><span class="line">        &#125;</span><br><span class="line">        System.out.println(<span class="string">&quot;]&quot;</span>);</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">        <span class="comment">// 进行数组反转</span></span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; arr.length / <span class="number">2</span>; i++) &#123;</span><br><span class="line">            <span class="comment">// 1：arr[0] &lt;==&gt; arr.length - 1</span></span><br><span class="line">            <span class="comment">// 2：arr[1] &lt;==&gt; arr.length - 2</span></span><br><span class="line">            <span class="comment">// 3：arr[2] &lt;==&gt; arr.length - 3</span></span><br><span class="line"></span><br><span class="line">            <span class="comment">// 创建中间临时变量</span></span><br><span class="line">            <span class="keyword">int</span> temp = arr[i];</span><br><span class="line">            arr[i] = arr[arr.length - i - <span class="number">1</span>];</span><br><span class="line">            arr[arr.length - i - <span class="number">1</span>] = temp;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        System.out.print(<span class="string">&quot;[&quot;</span>);</span><br><span class="line">        <span class="comment">// 通过循环打印数组</span></span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; arr.length; i++) &#123;</span><br><span class="line">            <span class="keyword">if</span> (i == arr.length - <span class="number">1</span>)&#123;</span><br><span class="line">                System.out.print(arr[i]);</span><br><span class="line">                <span class="keyword">break</span>;</span><br><span class="line">            &#125;</span><br><span class="line">            System.out.print(arr[i]+<span class="string">&quot;, &quot;</span>);</span><br><span class="line">        &#125;</span><br><span class="line">        System.out.println(<span class="string">&quot;]&quot;</span>);</span><br><span class="line"></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"></span><br></pre></td></tr></table></figure></li>
<li><p>数组扩容</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">GroupArray</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">        <span class="comment">/*</span></span><br><span class="line"><span class="comment">        * 通过随机数生(1-100)成一个长度为 5 的 int 数组</span></span><br><span class="line"><span class="comment">        *   对原有数组进行扩容操作(将原有数据放置在一个新的数组中((length+1)*2))</span></span><br><span class="line"><span class="comment">        *   在新的数组中继续将元素填满(随机数)</span></span><br><span class="line"><span class="comment">        * */</span></span><br><span class="line">        <span class="keyword">int</span>[] arr = <span class="keyword">new</span> <span class="keyword">int</span>[<span class="number">5</span>];</span><br><span class="line">        <span class="comment">// 通过循环生成长度为 10 的数组</span></span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; arr.length; i++) &#123;</span><br><span class="line">            <span class="comment">// 通过随机数进行赋值</span></span><br><span class="line">            arr[i] = (<span class="keyword">int</span>)(Math.random()*<span class="number">100</span>+<span class="number">1</span>);</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        System.out.print(<span class="string">&quot;[&quot;</span>);</span><br><span class="line">        <span class="comment">// 通过循环打印数组</span></span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; arr.length; i++) &#123;</span><br><span class="line">            <span class="keyword">if</span> (i == arr.length - <span class="number">1</span>)&#123;</span><br><span class="line">                System.out.print(arr[i]);</span><br><span class="line">                <span class="keyword">break</span>;</span><br><span class="line">            &#125;</span><br><span class="line">            System.out.print(arr[i]+<span class="string">&quot;, &quot;</span>);</span><br><span class="line">        &#125;</span><br><span class="line">        System.out.println(<span class="string">&quot;]&quot;</span>);</span><br><span class="line"></span><br><span class="line">        <span class="comment">// 创建新数组</span></span><br><span class="line">        <span class="keyword">int</span>[] newArr = <span class="keyword">new</span> <span class="keyword">int</span>[(arr.length+<span class="number">1</span>)*<span class="number">2</span>];</span><br><span class="line"></span><br><span class="line">        <span class="comment">// 通过循环取出原有数组的内容</span></span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; arr.length; i++) &#123;</span><br><span class="line">            newArr[i] = arr[i];</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="comment">// 将新的数组中的内容填满</span></span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = arr.length; i &lt; newArr.length; i++) &#123;</span><br><span class="line">            <span class="comment">// 通过随机数进行赋值</span></span><br><span class="line">            newArr[i] = (<span class="keyword">int</span>)(Math.random()*<span class="number">100</span>+<span class="number">1</span>);</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        System.out.print(<span class="string">&quot;[&quot;</span>);</span><br><span class="line">        <span class="comment">// 通过循环打印数组</span></span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; newArr.length; i++) &#123;</span><br><span class="line">            <span class="keyword">if</span> (i == newArr.length - <span class="number">1</span>)&#123;</span><br><span class="line">                System.out.print(newArr[i]);</span><br><span class="line">                <span class="keyword">break</span>;</span><br><span class="line">            &#125;</span><br><span class="line">            System.out.print(newArr[i]+<span class="string">&quot;, &quot;</span>);</span><br><span class="line">        &#125;</span><br><span class="line">        System.out.println(<span class="string">&quot;]&quot;</span>);</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"></span><br></pre></td></tr></table></figure></li>
</ul>
<h3 id="IDEA-中的调试功能"><a href="#IDEA-中的调试功能" class="headerlink" title="IDEA 中的调试功能"></a>IDEA 中的调试功能</h3><ul>
<li>DEBUG 就是计算机中排除故障的意思</li>
<li>通过点击虫子运行（Shift+f9）</li>
<li>可以在代码行设置断点(红色圆点)</li>
<li>在 DEBUG 模式中可以选择的功能<ul>
<li>step over：单步过</li>
<li>step into：单步入</li>
<li>force step into：单步强制进入</li>
<li>step out：单步出</li>
</ul>
</li>
</ul>
<h3 id="初识算法"><a href="#初识算法" class="headerlink" title="初识算法"></a>初识算法</h3><ul>
<li>数据结构是把数据在内存或磁盘存放的更加高效。好比图书馆的书架，格子已经帮我们订好了，我们只需要按照规定摆放书籍</li>
<li>算法就是具体的动作，是怎么摆放，怎么快速查找书籍</li>
<li>注意：我们目前应该着重于增强内功(编程思维)，算法只是一个武功秘籍，如果内功不够学习来很吃力的。甚至可能走火入魔</li>
<li>排序大致有以下几种：<ul>
<li>简单排序：选择排序、冒泡排序、插入排序</li>
<li>高级排序：快速排序、归并排序、希尔排序</li>
<li>特殊排序：桶排序、堆排序、基数排序、计数排序</li>
</ul>
</li>
</ul>
<h4 id="冒泡排序"><a href="#冒泡排序" class="headerlink" title="冒泡排序"></a>冒泡排序</h4><ul>
<li><p>它重复地走访过要排序的元素列，依次比较两个相邻的元素如果顺序（如从大到小、首字母从Z到A），错误就把他们交换过来。走访元素的工作是重复地进行，直到没有相邻元素需要交换。也就是说该元素列已经排序完成</p>
</li>
<li><p>冒泡排序原码：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@program</span>: javaSE</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@description</span>:</span></span><br><span class="line"><span class="comment"> *  冒泡排序</span></span><br><span class="line"><span class="comment"> *      它重复地走访过要排序的元素列，依次比较两个相邻的元素</span></span><br><span class="line"><span class="comment"> *      如果顺序（如从大到小、首字母从Z到A）错误就把他们交换过来</span></span><br><span class="line"><span class="comment"> *      走访元素的工作是重复地进行，直到没有相邻元素需要交换</span></span><br><span class="line"><span class="comment"> *      也就是说该元素列已经排序完成</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@author</span>: lan</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@create</span>: 2022-07-11 16:37</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@version</span>:1.0</span></span><br><span class="line"><span class="comment"> **/</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">BubbleSort</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">        <span class="comment">/**</span></span><br><span class="line"><span class="comment">         * 它会遍历若干次要排序的数列，每次遍历时</span></span><br><span class="line"><span class="comment">         * 它都会从前往后依次的比较相邻两个数的大小</span></span><br><span class="line"><span class="comment">         * 如果前者比后者大，则交换它们的位置</span></span><br><span class="line"><span class="comment">         * 这样，一次遍历之后，最大的元素就在数列的末尾</span></span><br><span class="line"><span class="comment">         * 采用相同的方法再次遍历时，第二大的元素就被排列在最大元素之前</span></span><br><span class="line"><span class="comment">         * 重复此操作，直到整个数列都有序为止</span></span><br><span class="line"><span class="comment">         */</span></span><br><span class="line">        <span class="keyword">int</span>[] arr = &#123;<span class="number">5</span>,<span class="number">2</span>,<span class="number">12</span>,<span class="number">57</span>,<span class="number">11</span>,<span class="number">59</span>,<span class="number">25</span>,<span class="number">20</span>&#125;;</span><br><span class="line">        <span class="comment">// &#123;3，1，2&#125; == 2</span></span><br><span class="line">        <span class="comment">// &#123;4，3，1，2&#125; == 3</span></span><br><span class="line">        <span class="comment">//</span></span><br><span class="line">        <span class="comment">// 从小到大排序</span></span><br><span class="line">        <span class="comment">// 外层循环控制元素移动的次数</span></span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; arr.length - <span class="number">1</span>; i++) &#123;</span><br><span class="line">            <span class="comment">// 内存循环将元素一个个放置数组的尾部</span></span><br><span class="line">            <span class="keyword">for</span> (<span class="keyword">int</span> j = <span class="number">0</span>; j &lt; arr.length - <span class="number">1</span> - i; j++) &#123;</span><br><span class="line">                <span class="comment">// 比较前后两个元素的大小</span></span><br><span class="line">                <span class="comment">// arr[j] VS arr[j+1]</span></span><br><span class="line">                <span class="keyword">if</span>(arr[j]&gt;arr[j+<span class="number">1</span>])&#123;</span><br><span class="line">                    <span class="comment">// 大的元素在小的元素后面</span></span><br><span class="line">                    <span class="keyword">int</span> temp = arr[j];</span><br><span class="line">                    arr[j] = arr[j+<span class="number">1</span>];</span><br><span class="line">                    arr[j+<span class="number">1</span>] = temp;</span><br><span class="line">                &#125;</span><br><span class="line">                System.out.println(Arrays.toString(arr));</span><br><span class="line">            &#125;</span><br><span class="line">            System.out.println(<span class="string">&quot;===============&quot;</span>);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br></pre></td></tr></table></figure></li>
</ul>
<h4 id="选择排序"><a href="#选择排序" class="headerlink" title="选择排序"></a>选择排序</h4><ul>
<li><p>选择排序（Selection sort）是一种简单直观的排序算法。它的工作原理是：第一次从待排序的数据元素中选出最小（或最大）的一个元素存放在序列的起始位置，然后再从剩余的未排序元素中寻找到最小（大）元素然后放到已排序的序列的末尾。以此类推，直到全部待排序的数据元素的个数为零选择排序是不稳定的排序方法</p>
</li>
<li><p>代码展示</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@program</span>: javaSE</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@description</span>:</span></span><br><span class="line"><span class="comment"> *  选择排序</span></span><br><span class="line"><span class="comment"> *      选择排序（Selection sort）是一种简单直观的排序算法</span></span><br><span class="line"><span class="comment"> *      它的工作原理是：第一次从待排序的数据元素中选出最小（或最大）的一个元素</span></span><br><span class="line"><span class="comment"> *      存放在序列的起始位置，然后再从剩余的未排序元素中寻找到最小（大）元素</span></span><br><span class="line"><span class="comment"> *      然后放到已排序的序列的末尾。以此类推，直到全部待排序的数据元素的个数为零</span></span><br><span class="line"><span class="comment"> *      选择排序是不稳定的排序方法</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@author</span>: lan</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@create</span>: 2022-07-11 17:20</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@version</span>:1.0</span></span><br><span class="line"><span class="comment"> **/</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">SelectSort</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">int</span>[] arr = &#123;<span class="number">5</span>,<span class="number">2</span>,<span class="number">12</span>,<span class="number">57</span>,<span class="number">11</span>,<span class="number">59</span>,<span class="number">25</span>,<span class="number">20</span>&#125;;</span><br><span class="line">        <span class="comment">// 外层循环控制元素调换次数</span></span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; arr.length - <span class="number">1</span>; i++) &#123;</span><br><span class="line">            <span class="keyword">int</span> maxIndex = <span class="number">0</span>;</span><br><span class="line">            <span class="comment">// 内存循环控制比较次数</span></span><br><span class="line">            <span class="keyword">for</span> (<span class="keyword">int</span> j = <span class="number">0</span>; j &lt; arr.length - i; j++) &#123;</span><br><span class="line">                <span class="comment">// 查找当前的最大值</span></span><br><span class="line">                <span class="keyword">if</span>(arr[j]&gt;arr[maxIndex])&#123;</span><br><span class="line">                    <span class="comment">// 记录最大值下标</span></span><br><span class="line">                    maxIndex = j;</span><br><span class="line">                &#125;</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="comment">// 内存循环结束找出最大值</span></span><br><span class="line">            <span class="comment">// 将最大值移动至数组的尾部</span></span><br><span class="line">            <span class="keyword">int</span> temp = arr[maxIndex];</span><br><span class="line">            arr[maxIndex] = arr[arr.length-<span class="number">1</span>-i];</span><br><span class="line">            arr[arr.length-<span class="number">1</span>-i] = temp;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        System.out.println(Arrays.toString(arr));</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"></span><br></pre></td></tr></table></figure></li>
</ul>
<h4 id="插入排序"><a href="#插入排序" class="headerlink" title="插入排序"></a>插入排序</h4><ul>
<li><p>插入排序，一般也被称为直接插入排序。对于少量元素的排序，它是一个有效的算法插入排序是一种最简单的排序方法。它的基本思想是将一个记录插入到已经排好序的有序表中，每增加一个新的记录数有序表长度增1</p>
</li>
<li><p>代码展示</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@program</span>: javaSE</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@description</span>:</span></span><br><span class="line"><span class="comment"> *  插入排序</span></span><br><span class="line"><span class="comment"> *      插入排序，一般也被称为直接插入排序</span></span><br><span class="line"><span class="comment"> *      对于少量元素的排序，它是一个有效的算法</span></span><br><span class="line"><span class="comment"> *      插入排序是一种最简单的排序方法</span></span><br><span class="line"><span class="comment"> *      它的基本思想是将一个记录插入到已经排好序的有序表中</span></span><br><span class="line"><span class="comment"> *      每增加一个新的记录数有序表长度增1</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@author</span>: lan</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@create</span>: 2022-07-11 17:24</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@version</span>:1.0</span></span><br><span class="line"><span class="comment"> **/</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">InsertSort</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">        <span class="keyword">int</span>[] arr = &#123;<span class="number">5</span>,<span class="number">2</span>,<span class="number">12</span>,<span class="number">57</span>,<span class="number">11</span>,<span class="number">59</span>,<span class="number">25</span>,<span class="number">20</span>&#125;;</span><br><span class="line"></span><br><span class="line">        <span class="comment">// 外层循环控制元素调换次数</span></span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">1</span>; i &lt; arr.length; i++) &#123;</span><br><span class="line">            <span class="comment">// 内存循环控制比较次数</span></span><br><span class="line">            <span class="keyword">for</span> (<span class="keyword">int</span> j = <span class="number">0</span>; j &lt; i; j++) &#123;</span><br><span class="line">                <span class="keyword">if</span>(arr[j] &gt; arr[i])&#123;</span><br><span class="line">                    <span class="keyword">int</span> temp = arr[i];</span><br><span class="line">                    arr[i] = arr[j];</span><br><span class="line">                    arr[j] = temp;</span><br><span class="line">                &#125;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        System.out.println(Arrays.toString(arr));</span><br><span class="line"></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure></li>
</ul>
<h4 id="二分查找"><a href="#二分查找" class="headerlink" title="二分查找"></a>二分查找</h4><ul>
<li><p>二分查找，它是一种效率较高的查找方法。但是，折半查找要求线性表必须采用顺序存储结构，而且表中元素按关键字有序排列。首先，假设表中元素是按升序排列，将表中间位置记录的关键字与查找关键字比较。如果两者相等，则查找成功，否则利用中间位置记录将表分成前、后两个子表。如果中间位置记录的关键字大于查找关键字，则进一步查找前一子表，否则进一步查找后一子表。重复以上过程，直到找到满足条件的记录，使查找成功。或直到子表不存在为止，此时查找不成功</p>
</li>
<li><p>代码示例</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@program</span>: javaSE</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@description</span>:</span></span><br><span class="line"><span class="comment"> *  二分查找</span></span><br><span class="line"><span class="comment"> *      它是一种效率较高的查找方法</span></span><br><span class="line"><span class="comment"> *      但是，折半查找要求线性表必须采用顺序存储结构</span></span><br><span class="line"><span class="comment"> *      而且表中元素按关键字有序排列</span></span><br><span class="line"><span class="comment"> *          首先，假设表中元素是按升序排列</span></span><br><span class="line"><span class="comment"> *          将表中间位置记录的关键字与查找关键字比较</span></span><br><span class="line"><span class="comment"> *          如果两者相等，则查找成功</span></span><br><span class="line"><span class="comment"> *          否则利用中间位置记录将表分成前、后两个子表</span></span><br><span class="line"><span class="comment"> *          如果中间位置记录的关键字大于查找关键字</span></span><br><span class="line"><span class="comment"> *          则进一步查找前一子表，否则进一步查找后一子表</span></span><br><span class="line"><span class="comment"> *          重复以上过程，直到找到满足条件的记录，使查找成功</span></span><br><span class="line"><span class="comment"> *          或直到子表不存在为止，此时查找不成功</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@author</span>: lan</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@create</span>: 2022-07-12 11:31</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@version</span>:1.0</span></span><br><span class="line"><span class="comment"> **/</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">BinarySearch</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">int</span>[] arr = <span class="keyword">new</span> <span class="keyword">int</span>[<span class="number">500000</span>];</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; arr.length; i++) &#123;</span><br><span class="line">            arr[i] = i+<span class="number">1</span>;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="comment">// 需要查找的数</span></span><br><span class="line">        <span class="keyword">int</span> target = <span class="number">0</span>;</span><br><span class="line"></span><br><span class="line">        <span class="comment">// 最左和最右的下标</span></span><br><span class="line">        <span class="keyword">int</span> left = <span class="number">0</span>;</span><br><span class="line">        <span class="keyword">int</span> right = arr.length - <span class="number">1</span>;</span><br><span class="line"></span><br><span class="line">        <span class="keyword">if</span>(arr[left] &gt; target || arr[right] &lt; target)&#123;</span><br><span class="line">            System.out.println(<span class="string">&quot;你查找的元素不在范围内&quot;</span>);</span><br><span class="line">        &#125;<span class="keyword">else</span> &#123;</span><br><span class="line">            <span class="comment">// 定义一个最终的查找下标</span></span><br><span class="line">            <span class="keyword">int</span> index = -<span class="number">1</span>;</span><br><span class="line">            <span class="keyword">while</span> (left&lt;=right)&#123;</span><br><span class="line">                <span class="comment">// 获取中间值下标</span></span><br><span class="line">                <span class="keyword">int</span> midden = (left + right) / <span class="number">2</span>;</span><br><span class="line">                <span class="keyword">if</span>(arr[midden] == target)&#123;</span><br><span class="line">                    index = midden;</span><br><span class="line">                    <span class="keyword">break</span>;</span><br><span class="line">                &#125;<span class="keyword">else</span> <span class="keyword">if</span>(arr[midden] &gt; target)&#123;</span><br><span class="line">                    <span class="comment">// 中间值比查找的值要大，修改右标记</span></span><br><span class="line">                    right = midden - <span class="number">1</span>;</span><br><span class="line">                &#125;<span class="keyword">else</span> &#123;</span><br><span class="line">                    <span class="comment">// 中间值比查找的值要小，修改左标记</span></span><br><span class="line">                    left = midden + <span class="number">1</span>;</span><br><span class="line">                &#125;</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">if</span> (index == -<span class="number">1</span>)&#123;</span><br><span class="line">                System.out.println(<span class="string">&quot;你查找的元素不在范围内&quot;</span>);</span><br><span class="line">            &#125;<span class="keyword">else</span> &#123;</span><br><span class="line">                System.out.println(<span class="string">&quot;您要查找的元素在&quot;</span>+index+<span class="string">&quot;位置&quot;</span>);</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"></span><br></pre></td></tr></table></figure></li>
</ul>
<h4 id="算法的性能"><a href="#算法的性能" class="headerlink" title="算法的性能"></a>算法的性能</h4><ul>
<li>目前而言只要大致了解一下</li>
<li>算法的性能往往会考虑两个方面<ul>
<li>时间维度：指算法所消耗的时间，一般称之为 时间复杂度</li>
<li>空间维度：指算法所占用的空间，一般称之为 空间复杂度</li>
</ul>
</li>
</ul>
<h5 id="时间复杂度"><a href="#时间复杂度" class="headerlink" title="时间复杂度"></a>时间复杂度</h5><ul>
<li><p>最经典的方法：大 O 符号表示， T(n) = O(f(n))</p>
</li>
<li><p>例如：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">for</span>(<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; n; i++)&#123;</span><br><span class="line">    j = i</span><br><span class="line">    j++;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 可以将 n 考虑为一个无限大的数字</span></span><br><span class="line"><span class="number">1</span>个时间颗粒 + 2n时间颗粒</span><br><span class="line">T(n) = (<span class="number">1</span>+2n)  ===&gt; T(n) = O(f(n)) ===&gt; O(n) = (<span class="number">1</span>+2n)</span><br><span class="line">    </span><br><span class="line"><span class="comment">// 将时间参考为无限大</span></span><br><span class="line">O(n)</span><br><span class="line"></span><br><span class="line"></span><br></pre></td></tr></table></figure></li>
<li><p>常见的时间复杂度</p>
<ul>
<li><p>常数阶：O(1)</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">int</span> i = <span class="number">0</span>;</span><br><span class="line"><span class="keyword">int</span> j = <span class="number">2</span>;</span><br><span class="line">i++;</span><br><span class="line">++j;</span><br><span class="line"></span><br></pre></td></tr></table></figure></li>
<li><p>对数阶：O(logN)</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">int</span> i = <span class="number">1</span>;</span><br><span class="line"><span class="keyword">while</span>(i &lt; n)&#123;</span><br><span class="line">    i = i * <span class="number">2</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure></li>
<li><p>线性阶：O(n)</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">for</span>(<span class="keyword">int</span> i = <span class="number">0</span>;i &lt; n; i++)&#123;</span><br><span class="line">    j = i;</span><br><span class="line">    j++;</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure></li>
<li><p>线性对数阶：O(nlogN)</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">for</span>(<span class="keyword">int</span> i = <span class="number">0</span>;i &lt; n; i++)&#123;</span><br><span class="line">    <span class="keyword">int</span> m = <span class="number">1</span>;</span><br><span class="line">    <span class="keyword">while</span>(m &lt; n)&#123;</span><br><span class="line">        m = m * <span class="number">2</span>;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure></li>
<li><p>平方阶：O(n²)</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">for</span>(<span class="keyword">int</span> i = <span class="number">0</span>;i &lt; n; i++)&#123;</span><br><span class="line">    <span class="keyword">for</span>(<span class="keyword">int</span> j = <span class="number">0</span>;j &lt; n; j++)&#123;</span><br><span class="line">        <span class="keyword">int</span> k = <span class="number">8</span>;</span><br><span class="line">        k++;</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"></span><br></pre></td></tr></table></figure></li>
<li><p>立方阶：O(n³)</p>
</li>
<li><p>K次方阶：O(n^k)</p>
</li>
<li><p>指数阶：O(2^n)</p>
</li>
<li><p>以上时间复杂度顺序，越往下越复杂，执行效率越低</p>
</li>
</ul>
</li>
<li><p>时间复杂度就是看循环的次数</p>
</li>
</ul>
<h5 id="空间复杂度"><a href="#空间复杂度" class="headerlink" title="空间复杂度"></a>空间复杂度</h5><ul>
<li><p>简单记忆：S(n)</p>
</li>
<li><p>空间复杂度：</p>
<ul>
<li><p>常数阶：S(1)</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">int</span> i = <span class="number">0</span>;</span><br><span class="line"><span class="keyword">int</span> j = <span class="number">2</span>;</span><br><span class="line">i++;</span><br><span class="line">++j;</span><br><span class="line"></span><br><span class="line"></span><br></pre></td></tr></table></figure></li>
<li><p>线性阶：S(n)</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 创建对象</span></span><br><span class="line"><span class="keyword">int</span>[] arr = <span class="keyword">new</span> <span class="keyword">int</span>[n];</span><br><span class="line"><span class="comment">// 空间复杂与循环无关</span></span><br><span class="line"><span class="keyword">for</span>(<span class="keyword">int</span> i = <span class="number">0</span>;i &lt; n; i++)&#123;</span><br><span class="line">    j = i;</span><br><span class="line">    j++;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"></span><br></pre></td></tr></table></figure></li>
<li><p>平方阶：S(n²)</p>
</li>
</ul>
</li>
<li><p>空间复杂度就是看对象的创建次数</p>
</li>
</ul>
<h4 id="二维数组"><a href="#二维数组" class="headerlink" title="二维数组"></a>二维数组</h4><ul>
<li><p>二维数组本质上就是以数组作为元素的数组，二维数组又称之为矩阵</p>
</li>
<li><p>二维数组创建实例代码</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">DoubleArray</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">        <span class="comment">// 二维数组创建 1</span></span><br><span class="line">        <span class="keyword">int</span>[][] arr1 = <span class="keyword">new</span> <span class="keyword">int</span>[<span class="number">5</span>][];</span><br><span class="line">        <span class="comment">// 二维数组创建 2</span></span><br><span class="line">        <span class="keyword">int</span>[][] arr2 = <span class="keyword">new</span> <span class="keyword">int</span>[<span class="number">5</span>][<span class="number">3</span>];</span><br><span class="line"></span><br><span class="line">        <span class="comment">// 给二维数组进行赋值</span></span><br><span class="line">        arr1[<span class="number">0</span>] = <span class="keyword">new</span> <span class="keyword">int</span>[]&#123;<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>,<span class="number">4</span>,<span class="number">5</span>&#125;;</span><br><span class="line">        arr1[<span class="number">1</span>] = <span class="keyword">new</span> <span class="keyword">int</span>[<span class="number">20</span>];</span><br><span class="line"></span><br><span class="line">        <span class="comment">// 二维数组静态创建</span></span><br><span class="line">        <span class="keyword">int</span>[][] arr3 = <span class="keyword">new</span> <span class="keyword">int</span>[][]&#123;&#123;<span class="number">1</span>,<span class="number">3</span>,<span class="number">4</span>&#125;,&#123;<span class="number">2</span>,<span class="number">98</span>&#125;,&#123;<span class="number">1</span>&#125;,&#123;<span class="number">3</span>,<span class="number">4</span>,<span class="number">5</span>,<span class="number">6</span>,<span class="number">7</span>&#125;&#125;;</span><br><span class="line">        <span class="keyword">int</span>[][] arr4 = &#123;&#123;<span class="number">1</span>,<span class="number">3</span>,<span class="number">4</span>&#125;,&#123;<span class="number">2</span>,<span class="number">98</span>&#125;,&#123;<span class="number">1</span>&#125;,&#123;<span class="number">3</span>,<span class="number">4</span>,<span class="number">5</span>,<span class="number">6</span>,<span class="number">7</span>&#125;&#125;;</span><br><span class="line"></span><br><span class="line">        <span class="comment">// 打印二维数组</span></span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; arr4.length; i++) &#123;</span><br><span class="line">            <span class="keyword">for</span> (<span class="keyword">int</span> j = <span class="number">0</span>; j &lt; arr4[i].length; j++) &#123;</span><br><span class="line">                System.out.print(arr4[i][j]+<span class="string">&quot; &quot;</span>);</span><br><span class="line">            &#125;</span><br><span class="line">            System.out.println(<span class="string">&quot;&quot;</span>);</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"></span><br></pre></td></tr></table></figure></li>
</ul>
<h5 id="二维数组实现杨辉三角"><a href="#二维数组实现杨辉三角" class="headerlink" title="二维数组实现杨辉三角"></a>二维数组实现杨辉三角</h5><ul>
<li><p>杨辉三角特点：</p>
<ul>
<li><p>每个数等于它上方两数之和</p>
</li>
<li><p>每行数字左右对称，由1开始逐渐变大</p>
</li>
<li><p>第n行的数字有n项</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">杨辉三角：</span><br><span class="line">		<span class="number">1</span></span><br><span class="line">       <span class="number">1</span> <span class="number">1</span></span><br><span class="line">      <span class="number">1</span> <span class="number">2</span> <span class="number">1</span></span><br><span class="line">     <span class="number">1</span> <span class="number">3</span> <span class="number">3</span> <span class="number">1</span></span><br><span class="line">    <span class="number">1</span> <span class="number">4</span> <span class="number">6</span> <span class="number">4</span> <span class="number">1</span></span><br><span class="line">   。。。。。。。</span><br><span class="line"></span><br></pre></td></tr></table></figure></li>
</ul>
</li>
<li><p>杨辉三角实现代码</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">YangHui</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">        <span class="comment">/*</span></span><br><span class="line"><span class="comment">        *</span></span><br><span class="line"><span class="comment">        * 使用二维数组打印一个 10 行杨辉三角</span></span><br><span class="line"><span class="comment">        *</span></span><br><span class="line"><span class="comment">        * 1</span></span><br><span class="line"><span class="comment">        * 1 1</span></span><br><span class="line"><span class="comment">        * 1 2 1</span></span><br><span class="line"><span class="comment">        * 1 3 3 1</span></span><br><span class="line"><span class="comment">        * 1 4 6 4 1</span></span><br><span class="line"><span class="comment">        * 1 5 10 10 5 1</span></span><br><span class="line"><span class="comment">        *</span></span><br><span class="line"><span class="comment">        * */</span></span><br><span class="line">        <span class="comment">// 创建一个二维数组</span></span><br><span class="line">        <span class="keyword">int</span>[][] arr = <span class="keyword">new</span> <span class="keyword">int</span>[<span class="number">10</span>][];</span><br><span class="line"></span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; arr.length; i++) &#123;</span><br><span class="line">            <span class="comment">// 使用循环给每层的数组赋值</span></span><br><span class="line">            arr[i] = <span class="keyword">new</span> <span class="keyword">int</span>[i+<span class="number">1</span>];</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="comment">// 生成杨辉三角</span></span><br><span class="line">        <span class="comment">// 外层循环遍历层数</span></span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; arr.length; i++) &#123;</span><br><span class="line">            <span class="comment">// 内层循环遍历每一层中的数字</span></span><br><span class="line">            <span class="keyword">for</span> (<span class="keyword">int</span> j = <span class="number">0</span>; j &lt; arr[i].length; j++) &#123;</span><br><span class="line">                <span class="keyword">if</span>(j==<span class="number">0</span>||j==arr[i].length-<span class="number">1</span>)&#123;</span><br><span class="line">                    arr[i][j] = <span class="number">1</span>;</span><br><span class="line">                &#125;<span class="keyword">else</span> &#123;</span><br><span class="line">                    <span class="comment">// 剩余部分规则：[i][j] = [i-1][j] + [i-1][j-1];</span></span><br><span class="line">                    arr[i][j] = arr[i-<span class="number">1</span>][j] + arr[i-<span class="number">1</span>][j-<span class="number">1</span>];</span><br><span class="line">                &#125;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; arr.length; i++) &#123;</span><br><span class="line">            <span class="keyword">for</span> (<span class="keyword">int</span> j = <span class="number">0</span>; j &lt; arr[i].length; j++) &#123;</span><br><span class="line">                System.out.print(arr[i][j]+<span class="string">&quot;\t\t&quot;</span>);</span><br><span class="line">            &#125;</span><br><span class="line">            System.out.println(<span class="string">&quot;&quot;</span>);</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure></li>
</ul>
<h2 id="java-内存模型初识"><a href="#java-内存模型初识" class="headerlink" title="java 内存模型初识"></a>java 内存模型初识</h2><ul>
<li><p>在java中我们的代码执行、对象创建，都是在虚拟机中处理的。那么对于虚拟机目前我们需要有一个基本的认知</p>
</li>
<li><p>在 JDK 1.6 之前 JVM 虚拟机结构</p>
<ul>
<li>栈(stack)<ul>
<li>存放的都是方法中的局部变量，每一次方法的调用的数据都是通过栈进行传递的，并且方法的运行一定是在栈中的</li>
<li>局部变量：方法中的变量和方法的参数</li>
<li>作用域：局部变量的作用域所属{}以内</li>
<li>如果变量超出了作用域，那么会立即从栈中消失</li>
</ul>
</li>
<li>堆(heap)<ul>
<li>凡是 new 出来的都在堆中</li>
<li>堆中的数据都有默认值：<ul>
<li>整数类型：0</li>
<li>浮点类型：0.0</li>
<li>字符类型：’\u0000’ (unicode 码表中的不可见字符)</li>
<li>布尔类型：false</li>
<li>引用类型：null</li>
</ul>
</li>
<li>堆也是垃圾收集器的主要区域，也称之为 GC堆</li>
</ul>
</li>
<li>方法区(method area)<ul>
<li>存储 .class 的相关信息，包含方法信息</li>
<li>用于存储已经被虚拟机加载的类信息和常量，静态变量，即时编译后的代码等等</li>
<li>方法区也会被称之为永久代，一个是标准，一个是实现</li>
</ul>
</li>
<li>本地方法栈(native method stack)<ul>
<li>与系统相关，和虚拟机栈比较类似，区别：虚拟机栈是执行 java 方法的，本地方法栈是执行本地的方法</li>
<li>本地方法都被 native 修饰</li>
</ul>
</li>
<li>寄存器(pc register)<ul>
<li>与 CPU 相关，包含程序计数器</li>
<li>控制程序执行的顺序，记录程序执行的时间等等</li>
</ul>
</li>
<li>总结：<ul>
<li><strong>栈：存放局部变量，方法的调用和数据传递都在栈中</strong></li>
<li><strong>堆：凡是 new 出来的，都在堆中</strong></li>
<li><strong>方法区：静态变量，常量，方法和类的相关信息</strong></li>
</ul>
</li>
</ul>
</li>
<li><p>在 JDK 1.7 中的改变：</p>
<ul>
<li><p>将 java 堆中开辟了一个空间，存放运行时常量池</p>
</li>
<li><p>移除方法区中的常量池</p>
<p><img src="../images/jdk1.7%E5%92%8Cjdk1.8%E5%86%85%E5%AD%98%E7%BB%93%E6%9E%84%E5%AF%B9%E6%AF%94.png" loading="lazy"></p>
</li>
</ul>
</li>
<li><p>在 JDK 1.8 中的改变：</p>
<ul>
<li>方法区被移除，取而代之的是 元空间</li>
<li>元空间直接使用内存</li>
<li>是为了融合 Hotsport 和 JRockit 而做出的改变</li>
</ul>
</li>
</ul>
<p><img src="../images/java%E5%86%85%E5%AD%98%E8%AF%A6%E7%BB%86%E5%9B%BE.png" loading="lazy"></p>
</div><div id="reward-container"><span class="hty-icon-button button-glow" id="reward-button" title="Donate" onclick="var qr = document.getElementById(&quot;qr&quot;); qr.style.display = (qr.style.display === &quot;none&quot;) ? &quot;block&quot; : &quot;none&quot;;"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-hand-coin-line"></use></svg></span><div id="reward-comment">I want you.</div><div id="qr" style="display:none;"><div style="display:inline-block"></div><div style="display:inline-block"></div><div style="display:inline-block"></div></div></div><ul class="post-copyright"><li class="post-copyright-author"><strong>Post author: </strong>zzl</li><li class="post-copyright-link"><strong>Post link: </strong><a href="http://zilongzeng.gitee.io/web/2022/09/13/java%E5%88%9D%E7%BA%A7/" title="java初级">http://zilongzeng.gitee.io/web/2022/09/13/java%E5%88%9D%E7%BA%A7/</a></li><li class="post-copyright-license"><strong>Copyright Notice: </strong>All articles in this blog are licensed under <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/deed.zh" target="_blank" rel="noopener" title="CC BY-NC-SA 4.0 "><svg class="icon"><use xlink:href="#icon-creative-commons-line"></use></svg><svg class="icon"><use xlink:href="#icon-creative-commons-by-line"></use></svg><svg class="icon"><use xlink:href="#icon-creative-commons-nc-line"></use></svg><svg class="icon"><use xlink:href="#icon-creative-commons-sa-line"></use></svg></a> unless otherwise stated.</li></ul></section></article><div class="post-nav"><div class="post-nav-item"><a class="post-nav-prev" href="/web/2022/10/07/java%E4%B8%AD%E7%BA%A7/" rel="prev" title="java中级"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-arrow-left-s-line"></use></svg><span class="post-nav-text">java中级</span></a></div><div class="post-nav-item"><a class="post-nav-next" href="/web/2022/05/24/ssm%E5%88%A9%E7%94%A8pageHelp%E5%AE%9E%E7%8E%B0%E5%88%86%E9%A1%B5/" rel="next" title="ssm利用pageHelp实现分页"><span class="post-nav-text">ssm利用pageHelp实现分页</span><svg class="icon" aria-hidden="true"><use xlink:href="#icon-arrow-right-s-line"></use></svg></a></div></div></div><div class="hty-card" id="comment"><div class="comment-tooltip text-center"><span>要不要和我说些什么？</span><br></div></div></main><footer class="sidebar-translate" id="footer"><div class="copyright"><span>&copy; 2019 – 2022 </span><span class="with-love" id="animate"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-cloud-line"></use></svg></span><span class="author"> zzl</span></div><div class="powered"><span>Powered by <a href="https://hexo.io" target="_blank" rel="noopener">Hexo</a> v5.4.0</span><span class="footer-separator">|</span><span>Theme - <a rel="noopener" href="https://github.com/YunYouJun/hexo-theme-yun" target="_blank"><span>Yun</span></a> v1.6.2</span></div></footer><a class="hty-icon-button" id="back-to-top" aria-label="back-to-top" href="#"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-arrow-up-s-line"></use></svg><svg class="progress-circle-container" viewBox="0 0 100 100"><circle class="progress-circle" id="progressCircle" cx="50" cy="50" r="48" fill="none" stroke="#0078E7" stroke-width="2" stroke-linecap="round"></circle></svg></a></div></body></html>